这两个 SQL 语句如何合并为一个?

How can those two SQL statements be combined into one?

我写了并且想结合这两个 sql,一个是基于另一个的结果。我检查了 this post,但看起来它不是基于结果的。我怎样才能实现它?

第一个sql:

SELECT
    `potential`.*,
    `customer`.`ID` as 'FID_customer'
FROM
    `os_potential` as `potential`,
    `os_customer` as `customer`
WHERE `potential`.`FID_author` = :randomID
      AND `potential`.`converted` = 1
      AND `potential`.`street` = `customer`.`street`
      AND `potential`.`zip` = `customer`.`zip`
      AND `potential`.`city` = `customer`.`city`;

第二个sql:

SELECT
    sum(`order`.`price_customer`) as 'Summe'
FROM
    `os_order` as `order`,
    `RESUTS_FROM_PREVIOUS_SQL_STATEMENT` as `results`
WHERE `order`.`FID_status` = 10
      AND `results`.`FID_customer` = `order`.`FID_customer`;

我想得到第一个 sql 的所有内容 + 第二个 sql 的 'Summe'。

表格

1.Potentials:

+----+------------+-----------+--------+-----+------+
| ID | FID_author | converted | street | zip | city |
+----+------------+-----------+--------+-----+------+

2.Customers:

+----+--------+-----+------+
| ID | street | zip | city |
+----+--------+-----+------+

3.Orders:

+----+--------------+----------------+
| ID | FID_customer | price_customer |
+----+--------------+----------------+

我认为你应该使用子选择,但要注意结果的数量,这不是性能的最佳选择。

你可以这样做:

SELECT n1, n2, (select count(1) from whatever_table) as n3, n4 from whatever_table 

请注意,子选择必须 return 只有 1 个结果,否则会出现错误

您只需像这样编写一个查询:

SELECT sum(o.price_customer) as Summe
FROM os_order o JOIN
     os_potential p JOIN
     os_customer c
     ON p.street = c.street AND p.zip = c.zip AND p.city = c.city JOIN
     os_order o2
     ON o2.FID_customer = c.FID_customer
WHERE p.FID_author = :randomID AND p.converted = 1 AND
      o2.FID_status = 10 ;

备注:

  • 从不FROM 子句中使用逗号。 始终ON 子句中使用带有条件的显式 JOIN 语法。
  • Table 别名越短越好。通常使用 table 名称的缩写。
  • 仅当 table/column 名称需要转义时才需要反引号。你的不需要转义。
SELECT p.*
     , c.ID FID_customer
     , o.summe
  FROM os_potential p
  JOIN os_customer c
    ON c.street = p.street 
   AND c.zip = p.zip 
   AND c.city = p.city 
  JOIN 
     ( SELECT FID_customer
            , SUM(price_customer) Summe
         FROM os_order 
        WHERE FID_status = 10
        GROUP
           BY FID_customer
     ) o
    ON o.FID_customer = c.ID
 WHERE p.FID_author = :randomID 
   AND p.converted = 1
   ;

如果第一个查询 return 每个客户 1 条记录,那么只需连接 3 个表,保留总和并使用 group by 子句:

SELECT
    `potential`.*,
    `customer`.`ID` as 'FID_customer',
    sum(`order`.`price_customer`) as Summe
FROM
    `os_potential` as `potential`
INNER JOIN
    `os_customer` as `customer`
ON  `potential`.`street` = `customer`.`street`
      AND `potential`.`zip` = `customer`.`zip`
      AND `potential`.`city` = `customer`.`city`
LEFT JOIN
    `os_order` as `order`
ON  `results`.`FID_customer` = `order`.`FID_customer`
      AND `order`.`FID_status` = 10
WHERE `potential`.`FID_author` = :randomID
      AND `potential`.`converted` = 1
GROUP BY `customer`.`ID`, <list all fields from potential table>

如果第一个查询可能 return 每个客户有多个记录,那么您需要在子查询中进行求和:

SELECT
    `potential`.*,
    `customer`.`ID` as 'FID_customer',
    `order`.Summe
FROM
    `os_potential` as `potential`
INNER JOIN
    `os_customer` as `customer`
ON  `potential`.`street` = `customer`.`street`
      AND `potential`.`zip` = `customer`.`zip`
      AND `potential`.`city` = `customer`.`city`
LEFT JOIN
    (SELECT FID_customer, sum(price_customer) as Summe
     FROM `os_order`
     WHERE FID_status=10 
     GROUP BY FID_customer
    ) as `order`
ON  `results`.`FID_customer` = `order`.`FID_customer`
WHERE `potential`.`FID_author` = :randomID
      AND `potential`.`converted` = 1