如何将这两条 SQL 语句合并为一条?
Posted
技术标签:
【中文标题】如何将这两条 SQL 语句合并为一条?【英文标题】:How can those two SQL statements be combined into one? 【发布时间】:2016-09-06 12:31:49 【问题描述】:我编写并想将这 2 个 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.潜力:
+----+------------+-----------+--------+-----+------+
| ID | FID_author | converted | street | zip | city |
+----+------------+-----------+--------+-----+------+
2.客户:
+----+--------+-----+------+
| ID | street | zip | city |
+----+--------+-----+------+
3.订单:
+----+--------------+----------------+
| ID | FID_customer | price_customer |
+----+--------------+----------------+
【问题讨论】:
如果不知道数据库的结构和表之间的关系,很难回答您的问题。你能解释一下吗? 看来你可能需要一个子查询。将来自另一个查询的结果用作需要它们作为表的查询的子查询。 @AndyKorneyev 我编辑了我的帖子并在我的查询下方显示了表格。 【参考方案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
;
【讨论】:
非常感谢。它正是我需要的。你能解释一下它是如何工作的吗? 我接受了第二个查询,并将其粘贴在第一个查询中。它被称为不相关子查询。【参考方案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
子句中使用逗号。 总是在ON
子句中使用带有条件的显式JOIN
语法。
表别名在较短时更易于理解。表名的缩写是常用的。
仅当需要转义表/列名时才需要反引号。你不需要逃避。
【讨论】:
order
是保留字(排序依据),在原始查询中必须用反引号括起来
@Shadow 虽然我同意order
对于表/列名称来说是一个糟糕的选择,但在这个特定的上下文中它不需要反引号。也就是说,我不相信这就是 OP 正在寻找的。span>
最后一次 JOIN os.order 应该是 os_order,我得到错误 Not unique table/alias: 'o'
,当我修复它时,有 uknown 列 'c.FID_customer',应该是 c.ID。当我修复所有这些时。我得到 1 列和 1 行的巨大数字。由于我真的是初学者,我什至不知道它来自哪里。但是非常感谢你的笔记。我愿意学习关节和良好的sql实践!【参考方案3】:
如果第一个查询返回每个客户 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
【讨论】:
【参考方案4】:我认为您应该使用子选择,但要注意结果的数量,这不是最好的性能。
你可以这样做:
SELECT n1, n2, (select count(1) from whatever_table) as n3, n4 from whatever_table
注意,子选择必须只返回 1 个结果,否则会出错
【讨论】:
以上是关于如何将这两条 SQL 语句合并为一条?的主要内容,如果未能解决你的问题,请参考以下文章