我有以下兩個表:返回一個表中的所有行,並與另一個表中的行的子集匹配?
rsrpID rsrpName
1 Library Catalog
2 Interlibrary Loan
3 Academic Search Complete
4 JSTOR
5 Project Muse
6 LibGuides
7 Web Resource
8 Other (please add to Notes)
9 Credo Reference
rsriID rsrirsrpID rsrisesdID
603 6 243
604 1 243
605 7 243
606 8 244
607 6 245
608 8 245
我試圖做的是恢復整個第一表,並在第二個表中的第一個表匹配rsrpID那些行,返回這些對相關行旁邊的第一個表,例如:
rsrpID rsrpName rsrisesdID
1 Library Catalog 243
2 Interlibrary Loan
3 Academic Search Complete
4 JSTOR
5 Project Muse
6 LibGuides 243
7 Web Resource 243
8 Other (please add to Notes)
9 Credo Reference
...但我不能爲我的生活弄清楚,會返回此連接語句。目前我得到的查詢是
select rp.rsrpID as ID, rp.rsrpName as Name,
(select if((count(rsrisesdID) > 0), 'checked', '')
from resourcesintroduced ri
where (ri.rsrirsrpID = rp.rsrpID)
and (rsrisesdID = 243)) as 'checked'
from resourcesintroduced ri,
resourcepool rp
where rsrisesdID = 243
group by ID
order by Name asc;
正如你可以看到,查詢是笨重,如果一個特定的rsrisesdID
沒有出現的話,那麼查詢都沒有返回行。
LEFT OUTER JOIN:這將有助於(http://www.codinghorror.com/blog/2007/10/a-visual-explanation- of-sql-joins.html)對你很好的描述。 – Ben