SQL join + group_concat 不返回某些行
SQL join + group_concat not returning some rows
在我的数据库中,我有下表:
People
+-----------+------------+
| IdPeople | Name |
+-----------+------------+
| 1 | James |
| 2 | Chris |
+-----------+------------+
ref
+---------+-------------+------+
| People | Color | Code |
+---------+-------------+------+
| 1 | 2 | 1 |
| 1 | 1 | 2 |
| 1 | 6 | 3 |
| 2 | 1 | 1 |
| 2 | 6 | 4 |
| 2 | 4 | NULL |
| 2 | 5 | NULL |
+---------+-------------+------+
Colors
+--------+--------------------+
| IdCol | Color |
+--------+--------------------+
| 1 | Blue |
| 2 | Green |
| 3 | Yellow |
| 4 | Red |
| 5 | Black |
| 6 | White |
+--------+--------------------+
Codes
+--------+----------------+
| IdCode | Code |
+--------+----------------+
| 1 | C++ |
| 2 | JavaScript |
| 3 | Python |
| 4 | HTML |
+--------+----------------+
我想加入所有表以获得类似这样的结果:
+------------+---------------------------+------------------------------------+
| Name | Color | Code |
+------------+---------------------------+------------------------------------+
| Chris | Blue, White, Red, Black | C++, HTML |
| James | Green, Blue, White | C++, JavaScript, Python |
+------------+---------------------------+------------------------------------+
我试过这样加入group_concat
SELECT People.Name,
group_concat(Colors.Color separator ", ") AS "Color",
group_concat(Codes.Code separator ", ") AS "Code"
FROM People
INNER JOIN ref ON People.IdPeople=ref.People
INNER JOIN Colors ON ref.Color=Colors.IdCol
INNER JOIN Codes ON ref.Code=Codes.Code
GROUP BY Name;
但是,由于 group_concat 没有 return 空行,我在 Chris 行中得到了除 Black 和 White 之外的所有内容。我不知道该怎么做,所以有人可以帮助我吗?
由于ref.code
可以为null,所以必须外连接代码table:LEFT OUTER JOIN Codes ON ...
.
完整查询:
SELECT People.Name,
group_concat(Colors.Color separator ", ") AS "Color",
group_concat(Codes.Code separator ", ") AS "Code"
FROM People
INNER JOIN REF ON People.IdPeople=ref.People
INNER JOIN Colors ON ref.Color=Colors.IdCol
LEFT JOIN Codes ON ref.Code=Codes.Code
GROUP BY Name;
在我的数据库中,我有下表:
People
+-----------+------------+
| IdPeople | Name |
+-----------+------------+
| 1 | James |
| 2 | Chris |
+-----------+------------+
ref
+---------+-------------+------+
| People | Color | Code |
+---------+-------------+------+
| 1 | 2 | 1 |
| 1 | 1 | 2 |
| 1 | 6 | 3 |
| 2 | 1 | 1 |
| 2 | 6 | 4 |
| 2 | 4 | NULL |
| 2 | 5 | NULL |
+---------+-------------+------+
Colors
+--------+--------------------+
| IdCol | Color |
+--------+--------------------+
| 1 | Blue |
| 2 | Green |
| 3 | Yellow |
| 4 | Red |
| 5 | Black |
| 6 | White |
+--------+--------------------+
Codes
+--------+----------------+
| IdCode | Code |
+--------+----------------+
| 1 | C++ |
| 2 | JavaScript |
| 3 | Python |
| 4 | HTML |
+--------+----------------+
我想加入所有表以获得类似这样的结果:
+------------+---------------------------+------------------------------------+
| Name | Color | Code |
+------------+---------------------------+------------------------------------+
| Chris | Blue, White, Red, Black | C++, HTML |
| James | Green, Blue, White | C++, JavaScript, Python |
+------------+---------------------------+------------------------------------+
我试过这样加入group_concat
SELECT People.Name,
group_concat(Colors.Color separator ", ") AS "Color",
group_concat(Codes.Code separator ", ") AS "Code"
FROM People
INNER JOIN ref ON People.IdPeople=ref.People
INNER JOIN Colors ON ref.Color=Colors.IdCol
INNER JOIN Codes ON ref.Code=Codes.Code
GROUP BY Name;
但是,由于 group_concat 没有 return 空行,我在 Chris 行中得到了除 Black 和 White 之外的所有内容。我不知道该怎么做,所以有人可以帮助我吗?
由于ref.code
可以为null,所以必须外连接代码table:LEFT OUTER JOIN Codes ON ...
.
完整查询:
SELECT People.Name,
group_concat(Colors.Color separator ", ") AS "Color",
group_concat(Codes.Code separator ", ") AS "Code"
FROM People
INNER JOIN REF ON People.IdPeople=ref.People
INNER JOIN Colors ON ref.Color=Colors.IdCol
LEFT JOIN Codes ON ref.Code=Codes.Code
GROUP BY Name;