以下sql查询有什么问题
select itemname from Item where itemid
in
((select ItemID
FROM Delivery NATURAL JOIN Supplier
WHERE SupplierName = 'Nepalese Corp.')
union
(select ItemID
FROM Sale NATURAL JOIN Department
WHERE DepartmentName = 'Navigation'))
我在本网站上看过另一篇文章,建议删除两个联合集上的内括号,并为第一个联合集设置一个别名。我试过了,MYSQL在下面显示的行显示了一个x,但是,查询运行正常。我的问题是发生了什么?
select itemname from Item where itemid
in
(select ItemID as id
FROM Delivery NATURAL JOIN Supplier
WHERE SupplierName = 'Nepalese Corp.'
union
select ItemID . //shows an x at this line
FROM Sale NATURAL JOIN Department
WHERE DepartmentName = 'Navigation')
答案 0 :(得分:1)
select itemname from Item
where itemid in
(
select ItemID
FROM Delivery
NATURAL JOIN Supplier WHERE SupplierName = 'Nepalese Corp.'
union
select ItemID
FROM Sale
NATURAL JOIN Department WHERE DepartmentName = 'Navigation'
)
您可以尝试以上代码。
只需删除不必要的括号即可解决问题。
答案 1 :(得分:0)
您可以尝试将内部结果加入主表
select itemname from Item as t1 join
(select distinct ItemID
FROM Delivery NATURAL JOIN Supplier
WHERE SupplierName = 'Nepalese Corp.'
union
select distinct ItemID
FROM Sale NATURAL JOIN Department
WHERE DepartmentName = 'Navigation') as t2 on t1.ItemID=t2.ItemID