![](/img/trans.png)
[英]LEFT JOIN two tables so that left table data is filtered but right table data is displayed even if left table data does not exist
[英]right join even if row on second table does not exist
我想加入两个表,即使第二个表没有匹配。
表用户:
uid | name
1 dude1
2 dude2
表帐户:
uid | accountid | name
1 1 account1
我想要的表:
uid | username | accountname
1 dude1 account1
2 dude2 NULL
我正在尝试的查询:
SELECT user.uid as uid, user.name as username, account.name as accountname
FROM user RIGHT JOIN account ON user.uid=accout.uid
我得到了什么:
uid | username | accountname
1 dude1 account1
请改用Left Join
SELECT user.uid as uid, user.name as username, account.name as accountname
FROM user LEFT JOIN account ON user.uid=accountid.uid
尝试使用LEFT JOIN
查询
SELECT user.uid as uid, user.name as username, account.name as accountname
FROM user
LEFT JOIN account
ON user.uid=accout.uid
我想看看JOIN
查询的这种直观表示
右连接保留第二个表中的所有结果(它将所有结果保留在右侧表中),您需要左连接,或者在连接子句中交换用户和帐户的位置。
SELECT user.uid as uid, user.name as username, account.name as accountname
FROM user LEFT JOIN account ON user.uid=account.uid
我相信应该这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.