一尘不染

那两个SQL语句如何组合成一个?

sql

我写了,想结合这两个sql,一个是基于另一个的结果。

第一个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.潜力:

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

2.客户:

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

3.订单:

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

阅读 275

收藏
2021-05-30

共1个答案

一尘不染

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
;

2021-05-30