我有以下两个表:
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
根本没有出现特定的查询,则查询根本不会返回任何行。