查询连接 table 时,有没有办法 return 空值行

Is there way to return null valued rows when when querying joined table

我有两个 table:users table 有 idname 列和 events table 有idcontentuserId 列。

我正在尝试查询一个 table,其中 return 将来自这两个 table 的信息与 nameevents 列相结合,其中 events 将表示对应于用户的 content 字段数组。

这是我的查询 运行:

select 
    name, group_concat(content) as events 
from 
    users 
left join 
    events on id = userId 
group by 
    userId 
order by 
    id

但是,具有 null 值的行不会被 returned 除了只有一行。我做错了什么?

用户table

[
{
"id": 1,
"name": "Hugo Powlowski"
},
{
"id": 2,
"name": "Jeremy Littel II"
},
{
"id": 3,
"name": "Eleanor King"
},
{
"id": 4,
"name": "Rogelio Jacobson"
},
{
"id": 5,
"name": "Jerald Rowe PhD"
},
{
"id": 6,
"name": "Robyn Tromp"
},
{
"id": 7,
"name": "Norman Zboncak"
},
{
"id": 8,
"name": "Mr. Kristy Orn"
},
{
"id": 9,
"name": "Mrs. Olivia Trantow"
},
{
"id": 10,
"name": "Daniel Lebsack"
}
]

事件table

[
{
"eventId": 3,
"content": "hello",
"userId": 7
},
{
"eventId": 12,
"content": "rulsan berden",
"userId": 1
}
]

加入table

[
{
"name": "Hugo Powlowski",
"events": "rulsan berden"
},
{
"name": "Jeremy Littel II",
"events": null
},
{
"name": "Norman Zboncak",
"events": "hello"
}
]

尝试使用嵌套的 SELECT,这应该 return null 对于没有任何事件的 users

select 
    u.name, 
    SELECT(
      group_concat(content)  
        FROM 
      events 
        WHERE 
      userId = u.id
    ) as events 
from 
    users u
order by 
    u.id

您应该按父 table 中的列分组,而不是左连接的 table,这样值永远不会为空。

因此将 GROUP BY userid 更改为 GROUP BY users.id