这个关联两张表的sql语句该怎么写?
有两张表
table 1
id name
1 text1
2 text2
3 text3
4 text4
table2
id tb1id content
1 2 content1
2 3 content2
其中table1的id 与table2的tblid相对应(但是table1中的ID在table2中不一定有的)
我要取出以下数据:
-----------------------
1 text1
2 text2 content1
3 text4 content2
4 text5
用select a.id,a.name,b.content from table1 a,table2 b where a.id=b.tb1id
只能取出两条数据
问题点数:60、回复次数:8Top
1 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2005-02-04 11:47:16 得分 0
select
a.id,
a.name,
b.content
from
table1 a
left join
table2 b
on
a.id=b.tb1idTop
2 楼skyboy0720(曲终人散)回复于 2005-02-04 11:47:24 得分 10
select a.id,a.name,b.content from table1 a left join table2 b on a.id=b.tb1id
Top
3 楼llhly(无名)回复于 2005-02-04 11:54:55 得分 0
select a.id,a.name,b.content,b.tb1id from table1 a,table2 b where a.id=b.tb1id and a.id in (select tb1id from table2)Top
4 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2005-02-04 11:56:34 得分 40
select a.id,a.name,b.content from table1 a,table2 b where a.id*=b.tb1idTop
5 楼xluzhong(Ralph)回复于 2005-02-04 11:59:53 得分 10
select a.id,a.name,b.content
from table1 a
left join
table2 b
where a.id=b.tb1id
Top
6 楼xluzhong(Ralph)回复于 2005-02-04 12:00:03 得分 0
select a.id,a.name,b.content
from table1 a
left join
table2 b
where a.id=b.tb1idTop
7 楼xluzhong(Ralph)回复于 2005-02-04 12:00:59 得分 0
或者
select a.id,a.name,b.content from table1 a,table2 b where a.id=b.tb1id
union
select a.id,a.name,b.content from table1 a,table2 b where a.id<>b.tb1idTop
8 楼tgwandqn(yaterman)回复于 2005-02-04 12:19:35 得分 0
up
Top




