是否可以在沒有任何子查詢的情況下執行此查詢?或者更少的代碼?獲得相同的結果。查詢沒有子查詢? SQL
SELECT person
FROM tbla
WHERE person IN
(SELECT person
FROM tblb
WHERE age IN
(SELECT age
FROM tblc
where age = '20'));
是否可以在沒有任何子查詢的情況下執行此查詢?或者更少的代碼?獲得相同的結果。查詢沒有子查詢? SQL
SELECT person
FROM tbla
WHERE person IN
(SELECT person
FROM tblb
WHERE age IN
(SELECT age
FROM tblc
where age = '20'));
SELECT tbla.person
FROM tbla
inner join tblb on tblb.person = tbla.person
inner join tblc on tblc.age = tblb.age
WHERE tblc.age = '20'
可以使用加入,但一個DISTINCT
關鍵字是必需的,以消除重複行。
SELECT DISTINCT a.*
FROM tbla a
INNER JOIN tblb b
ON a.person = b.person
INNER JOIN tblc c
ON b.age = c.age
WHERE c.age = 20
爲了進一步獲得更多的知識有關加入,請訪問以下鏈接:
只要把你的tblc
和tblb
作爲INNER JOIN
:
SELECT tbla.person
FROM tbla
INNER JOIN tblb
ON tblb.person = tbla.person
INNER JOIN tblc
ON tblc.age = tblb.age
WHERE tblc.age = 20;
如果不知道所選表格的結構,則無法真正回答這個問題。 – DanSingerman 2013-03-27 14:11:05
謝謝大家的快速回復。像魅力一樣工作。謝謝! – kvambaam 2013-03-27 14:24:52