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

时间:2016-09-06 12:31:49

标签: mysql sql mariadb

我写了并想结合这两个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 +' Summe'中获取所有内容。来自第二个sql。

TABLES

1.Potentials:

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

2.Customers:

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

3.Orders:

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

4 个答案:

答案 0 :(得分:3)

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
   ;

答案 1 :(得分:2)

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

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子句中使用逗号。 始终JOIN子句中的条件使用显式ON语法。
  • 表别名在短缺时更容易理解。通常使用表名的缩写。
  • 仅在需要转义表/列名称时才需要反引号。你的不需要逃脱。

答案 2 :(得分:1)

如果第一个查询为每个客户返回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>

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

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

答案 3 :(得分:0)

我认为你应该使用一个子选择,但要注意结果的数量,它不是最好的表现。

您可以这样做:

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

请注意,subselect必须只返回1个结果,否则你会有错误