MySQL JOIN行(如果存在),否则用NULL填充 [英] MySQL JOIN rows if existing else fill with NULL

查看:267
本文介绍了MySQL JOIN行(如果存在),否则用NULL填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个人的名字和属性表.有些人还没有条目,我想创建所有人员及其属性(如果有)的完整列表,而其他人的属性列为"NULL".这是我的简化示例:

I have two tables of peoples names and properties. Some have no entries yet and I would like to create a full list of all people and their properties (if available) else have them listed with property "NULL". That's my simplicified example:

名称

id     name
-------------
1      Jon
2      Peter
3      Sam
4      Bruce
...

属性

names_id    property
--------------------
1           foo
3           bar
...

所需结果:

id     name     property
------------------------
1      Jon      foo
2      Peter    NULL
3      Sam      bar
4      Bruce    NULL
...

谢谢!

推荐答案

在这里,使用join:

  select A.id, A.name, B.property
    from table1 A
    left outer join table2  B on A.id=B.names_id    

这篇关于MySQL JOIN行(如果存在),否则用NULL填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆