如何从三个表中获取值

时间:2013-10-10 07:49:55

标签: sql-server sql-server-2008 join

我有3张桌子。喜欢:B001,B002,B003

每个表格都有Entrynumber列。

所以我必须在所有3个表中找到Entrynumber值。

就像我们为一张桌子写的一样:

select count(*) from B001  Where Entrynumber ='3340'

我想知道加入表格的最佳方式是什么。

3 个答案:

答案 0 :(得分:1)

使用内部联接

select count(*) 
from B001 b1
inner join B002 b2 on b1.Entrynumber = b2.Entrynumber 
inner join B003 b3 on b1.Entrynumber = b3.Entrynumber 
Where b1.Entrynumber ='3340'

获取所有表中Entrynumber存在的记录数。

答案 1 :(得分:0)

在所有表格中找到值

 select
      (select count(*) from b001 where EntryNumber = '3340') b1Count,
      (select count(*) from b002 where EntryNumber = '3340') b2Count,
      (select count(*) from b003 where EntryNumber = '3340') b3Count

答案 2 :(得分:0)

SELECT COUNT(*) FROM B001
INNER JOIN B002 ON (B001.Entrynumber = B002.Entrynumber AND B002.Entrynumber = '3340')
INNER JOIN B003 ON (B001.Entrynumber = B003.Entrynumber AND B003.Entrynumber = '3340')
WHERE B001.Entrynumber = '3340'

内部联接,条件是最好的方式。