Microsoft SQL 服务器中两个伪(派生)表之间的交集
INTERSECT between two pseudo (derived) tables in Microsoft SQL server
我正在尝试找出具有一列的两个伪 table 之间是否存在任何差异(查看一个 table 中的每个元素是否都在另一个中)
代码:
SELECT *
FROM
(SELECT Orders.OrderID
FROM (((Categories FULL OUTER JOIN Products on Categories.CategoryID = Products.CategoryID)
FULL OUTER JOIN OrderDetails on OrderDetails.ProductID = Products.ProductID)
FULL OUTER JOIN Orders on Orders.OrderID = OrderDetails.OrderID)
FULL OUTER JOIN Suppliers on Suppliers.SupplierID = Products.SupplierID
WHERE CategoryName LIKE 'Dairy%' AND country ='France')
INTERSECT
(SELECT Orders.OrderID
FROM (((Categories LEFT JOIN Products on Categories.CategoryID = Products.CategoryID)
LEFT JOIN OrderDetails on OrderDetails.ProductID = Products.ProductID)
LEFT JOIN Orders on Orders.OrderID = OrderDetails.OrderID)
LEFT JOIN Suppliers on Suppliers.SupplierID = Products.SupplierID
WHERE CategoryName LIKE 'Dairy%' AND country ='France')
输出示例:
查询的顶部给出了前三个结果:
OrderID
10255
10267
10275
代码的第二部分(INTERSECT 下面)给出:
OrderID
10255
10267
10275
现在由于某些原因代码不是 运行 交集。该代码单独工作,为我提供 INSERSECT 上方和下方的子查询的各个列,但整体上它不起作用。我收到的错误消息是:
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'INTERSECT'.
Completion time: 2022-05-23T14:10:06.3560367+01:00
知道如何解决这个问题吗?如果有帮助,INTERSECT 带有红色下划线。
谢谢。
只需删除 FROM 和外括号,如下所示:
SELECT Orders.OrderID
FROM (((Categories FULL OUTER JOIN Products on Categories.CategoryID = Products.CategoryID)
FULL OUTER JOIN OrderDetails on OrderDetails.ProductID = Products.ProductID)
FULL OUTER JOIN Orders on Orders.OrderID = OrderDetails.OrderID)
FULL OUTER JOIN Suppliers on Suppliers.SupplierID = Products.SupplierID
WHERE CategoryName LIKE 'Dairy%' AND country ='France'
INTERSECT
SELECT Orders.OrderID
FROM (((Categories LEFT JOIN Products on Categories.CategoryID = Products.CategoryID)
LEFT JOIN OrderDetails on OrderDetails.ProductID = Products.ProductID)
LEFT JOIN Orders on Orders.OrderID = OrderDetails.OrderID)
LEFT JOIN Suppliers on Suppliers.SupplierID = Products.SupplierID
WHERE CategoryName LIKE 'Dairy%' AND country ='France'
问题是您有一个 FROM 子句试图 运行 括号内包含 2 个子选择。
SELECT
*
FROM
(SELECT 1 AS a)
产生错误:
消息 102,级别 15,状态 1,第 4 行
')' 附近的语法不正确。
但是,添加别名效果很好:
SELECT
*
FROM
(SELECT 1 AS a) as a
更新:
您只需为第一个查询设置别名:
SELECT
*
FROM
(SELECT 1 AS a) as a
INTERSECT
(SELECT 1 AS a)
或者,按照 RBarryYoung 的建议,完全删除括号。
只需在 INTERSECT 起作用之前放一个 x。即:
SELECT *
FROM
(SELECT Orders.OrderID
FROM (((Categories FULL OUTER JOIN Products on Categories.CategoryID = Products.CategoryID)
FULL OUTER JOIN OrderDetails on OrderDetails.ProductID = Products.ProductID)
FULL OUTER JOIN Orders on Orders.OrderID = OrderDetails.OrderID)
FULL OUTER JOIN Suppliers on Suppliers.SupplierID = Products.SupplierID
WHERE CategoryName LIKE 'Dairy%' AND country ='France') x
INTERSECT
(SELECT Orders.OrderID
FROM (((Categories LEFT JOIN Products on Categories.CategoryID = Products.CategoryID)
LEFT JOIN OrderDetails on OrderDetails.ProductID = Products.ProductID)
LEFT JOIN Orders on Orders.OrderID = OrderDetails.OrderID)
LEFT JOIN Suppliers on Suppliers.SupplierID = Products.SupplierID
WHERE CategoryName LIKE 'Dairy%' AND country ='France')
我正在尝试找出具有一列的两个伪 table 之间是否存在任何差异(查看一个 table 中的每个元素是否都在另一个中)
代码:
SELECT *
FROM
(SELECT Orders.OrderID
FROM (((Categories FULL OUTER JOIN Products on Categories.CategoryID = Products.CategoryID)
FULL OUTER JOIN OrderDetails on OrderDetails.ProductID = Products.ProductID)
FULL OUTER JOIN Orders on Orders.OrderID = OrderDetails.OrderID)
FULL OUTER JOIN Suppliers on Suppliers.SupplierID = Products.SupplierID
WHERE CategoryName LIKE 'Dairy%' AND country ='France')
INTERSECT
(SELECT Orders.OrderID
FROM (((Categories LEFT JOIN Products on Categories.CategoryID = Products.CategoryID)
LEFT JOIN OrderDetails on OrderDetails.ProductID = Products.ProductID)
LEFT JOIN Orders on Orders.OrderID = OrderDetails.OrderID)
LEFT JOIN Suppliers on Suppliers.SupplierID = Products.SupplierID
WHERE CategoryName LIKE 'Dairy%' AND country ='France')
输出示例:
查询的顶部给出了前三个结果:
OrderID
10255
10267
10275
代码的第二部分(INTERSECT 下面)给出:
OrderID
10255
10267
10275
现在由于某些原因代码不是 运行 交集。该代码单独工作,为我提供 INSERSECT 上方和下方的子查询的各个列,但整体上它不起作用。我收到的错误消息是:
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'INTERSECT'.
Completion time: 2022-05-23T14:10:06.3560367+01:00
知道如何解决这个问题吗?如果有帮助,INTERSECT 带有红色下划线。
谢谢。
只需删除 FROM 和外括号,如下所示:
SELECT Orders.OrderID
FROM (((Categories FULL OUTER JOIN Products on Categories.CategoryID = Products.CategoryID)
FULL OUTER JOIN OrderDetails on OrderDetails.ProductID = Products.ProductID)
FULL OUTER JOIN Orders on Orders.OrderID = OrderDetails.OrderID)
FULL OUTER JOIN Suppliers on Suppliers.SupplierID = Products.SupplierID
WHERE CategoryName LIKE 'Dairy%' AND country ='France'
INTERSECT
SELECT Orders.OrderID
FROM (((Categories LEFT JOIN Products on Categories.CategoryID = Products.CategoryID)
LEFT JOIN OrderDetails on OrderDetails.ProductID = Products.ProductID)
LEFT JOIN Orders on Orders.OrderID = OrderDetails.OrderID)
LEFT JOIN Suppliers on Suppliers.SupplierID = Products.SupplierID
WHERE CategoryName LIKE 'Dairy%' AND country ='France'
问题是您有一个 FROM 子句试图 运行 括号内包含 2 个子选择。
SELECT
*
FROM
(SELECT 1 AS a)
产生错误:
消息 102,级别 15,状态 1,第 4 行 ')' 附近的语法不正确。
但是,添加别名效果很好:
SELECT
*
FROM
(SELECT 1 AS a) as a
更新:
您只需为第一个查询设置别名:
SELECT
*
FROM
(SELECT 1 AS a) as a
INTERSECT
(SELECT 1 AS a)
或者,按照 RBarryYoung 的建议,完全删除括号。
只需在 INTERSECT 起作用之前放一个 x。即:
SELECT *
FROM
(SELECT Orders.OrderID
FROM (((Categories FULL OUTER JOIN Products on Categories.CategoryID = Products.CategoryID)
FULL OUTER JOIN OrderDetails on OrderDetails.ProductID = Products.ProductID)
FULL OUTER JOIN Orders on Orders.OrderID = OrderDetails.OrderID)
FULL OUTER JOIN Suppliers on Suppliers.SupplierID = Products.SupplierID
WHERE CategoryName LIKE 'Dairy%' AND country ='France') x
INTERSECT
(SELECT Orders.OrderID
FROM (((Categories LEFT JOIN Products on Categories.CategoryID = Products.CategoryID)
LEFT JOIN OrderDetails on OrderDetails.ProductID = Products.ProductID)
LEFT JOIN Orders on Orders.OrderID = OrderDetails.OrderID)
LEFT JOIN Suppliers on Suppliers.SupplierID = Products.SupplierID
WHERE CategoryName LIKE 'Dairy%' AND country ='France')