首页 > 解决方案 > 使用 1 个 where 子句联合选择 2 个 SQL 表

问题描述

我有 2 张桌子CustomerProduct. 我想将它组合到每个表的价格 > 1000 的结果集中。

但问题是我是否有办法为WHEREthe 使用 1 个子句,UNION或者我需要为每个表创建一个子句。

例子:

SELECT ProductName, Customer, Price
FROM Customer
WHERE Price > 1000

UNION

SELECT ProductName, Company, Price
FROM Products
WHERE Price > 1000

ORDER BY Price

有没有办法让它看起来更像这样:

SELECT ProductName, Customer, Price
FROM Customer

UNION

SELECT ProductName, Company, Price
FROM Products

WHERE Price > 1000
ORDER BY Price

我从这个 SQL 中得到一个错误。

谢谢你的帮助。

标签: sql

解决方案


推荐阅读