对不起,英语不好。
所以我有一个选择SQL返回结果是这样的:
Aid type name
1 1 a
2 1 b
3 2 c
4 2 d
5 3 e
6 3 f
7 3 g
另一个select返回如下结果:
Bid Aid type content key
3 5 3 aaa 1
9 7 3 bbb 1
10 10 2 ccc 2
我想得到这个:
Aid Bid type name content
5 3 3 e aaa
6 NULL 3 f NULL
7 9 3 g bbb
我怎样才能做到这一点?
select Aid, type, name from tableA
(HOW to JOIN???) (
select Bid, type, content from tableB where key = 1
) on tableA.Aid = tableB.Aid
有一些很好的教程上加入W3Schools的,但假设你想加入t1
与t2
使用Aid
的关键:
SELECT t1.Aid, t2.Bid, t1.type, t1.name, t2.content
FROM t1
LEFT JOIN t2 ON t1.Aid = t2.Aid
WHERE (t2.key = 1) OR (t2.key IS NULL)
请注意,您需要使用LEFT JOIN
,因为没有匹配Aid
的记录t2
在每一个记录t1
,并要显示NULL
比。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句