mysql - 即使第二个表上的行不存在也右连接

标签 mysql sql

我想加入两个表,即使第二个表没有匹配项。

表用户:

uid | name
1     dude1
2     dude2

表帐户:

uid | accountid | name
1     1           account1

我想要的 table :

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

最佳答案

改用左连接

SELECT user.uid as uid, user.name as username, account.name as accountname
FROM user LEFT JOIN account ON user.uid=accountid.uid

关于mysql - 即使第二个表上的行不存在也右连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16987322/

相关文章:

mysql - 仅获取 a 列具有与 b 列中的独特值一样多的重复条目的行

php ADODB 在连接到 MySQL 之前调用 mysql_options()

php - SQL 显示未读消息数...包括未读回复

sql - 甲骨文文本 - "user filter command exited with status 127"

mysql - SQL IN() 子句按字母顺序执行列

sql - 如何在 SQL 唯一约束中仅使用 DateTime 列的年份?

php - 转换从数据库中检索到的日期时间

mysql - 执行数学函数作为默认值 MySQL

sql - 如何截断和收缩日志文件?

sql - postgresql 查询