mysql的left join、right join、inner join
id, userName, cardNo
1 aaa 111
2 bbb 111
3 aaa 222
B加油记载表:
id, number, userName , cardNo,
1 1234 aaa 111
2 234 bbb 111
left join:
select * from B b left join A a on a.userName = b.userName where b.userName=aaa
由于上面sql中,on后面的前提,userName在A表中对应多条,而不是对应一条,结果集就是笛卡尔积。B表中的1条知足剩余A表中的2条知足。结果为2条。
select * from B b left join A a on a.userName = b.userName and a.cardNo = b.cardNo where b.userName=aaa
由于上面sql中,on后面的两个前提在A表中只能寻到一条独一数据,所以结果就是B表中有多少条数据知足where,结果集就返回多少条数据。这里是返回一条数据
right join:
下面这个sql与上面的left join结果一样:
select * from A a right join B b on a.userName = b.userName and a.cardNo = b.cardNo where b.userName=aaa
inner join:
select * from A a inner join B b on a.userName = b.userName and a.cardNo = b.cardNo where a.userName=aaa
还是第一看on后面的前提,假如A表中的一条数据对应on的两个前提在B中只要一条数据,则返回知足where前提的2条数据。
select * from B b inner join A a on a.userName = b.userName and a.cardNo = b.cardNo where a.userName=aaa
以上总结一条:看on后面的前提,在被关联表中的数据是一条还是多条。
更多相关问题请拜访PHP中文网:mysql视频教程
以上就是mysql的left join、right join、inner join的具体内容,更多请关注百分百源码网其它相关文章!