select art.artno, art.name from Art
left outer join store on art.artno =store.artno
where art.Artno not in (select art.artno from store)
查询应该是这样的,但似乎不起作用(我没有在列名旁边得到任何结果行)。使用MSSQL 2008.
table art table store EXPECTED RESULT
artno name artno qty artno name
1 xbox360 1 1 2 XHW
2 XHW 3 2 5 PS2
3 NETANDO 4 1
5 PS2 6 3
6 PS1
4 X1
如何编写查询以获取示例中显示的Expected out?
只是为了让你知道表格是100plus K行大如果有帮助。
最后说明为什么上面的代码不起作用将是有益的。我看着这个this link似乎外连接必须工作,可能我根本无法理解这一点。
我也试过full outer join
,没有帮助。使用except
我只能找到artno
,但没有让我生成name
列。
答案 0 :(得分:1)
另一种方法可能是
select
a.`artno`,
a.`name`
from
art a
left join
store s on s.artno=a.artno
where
s.artno is null
在大型桌面上,第二种方法很可能会更好。
答案 1 :(得分:0)
select art.artno, art.name from art
where art.artno not in (select store.artno from store)
您无需进行任何类型的连接即可使用内部查询(在本例中为(select store.artno from store)
)。内部查询就像构建一个临时表,用值填充它,并在查询中使用它。
由于您希望内部查询在表artno
中为您提供所有store
,因此您应该使用(select store.artno from store)
而不是(select art.artno from store)
,因为我认为这会选择来自外部查询的art.artno
,不考虑store
表的内容。
答案 2 :(得分:0)
Georges答案有效,但在该大小的表格中,具有“不存在”的相关子查询将更快。
经测试,我的子查询比左外部慢。这是前进的方向