SQL 使用 GROUP_CONCAT() 函数查询
SQL query using GROUP_CONCAT() function
我有两个表:
1.Activity-- which stores all the activities from a user
2.User-- which stores all the registered users
我想在一个查询中获取所有 post 和所有 post 的类似内容。一行是 post 还是类似由 type 列的值决定。
Table 姓名 - Activity
| actId | parentId | type | postedby |
---------------------------------------------------
| 100 | NULL | post | 100 |
| 200 | 100 | like | 200 |
| 300 | 100 | like | 300 |
| 400 | 100 | like | 400 |
| 500 | NULL | post | 100 |
| 600 | 500 | like | 600 |
Table 姓名 - 用户
| userId | name |
-------------------
| 100 | Amit |
| 200 | Alok |
| 300 | Arjun |
| 400 | Arpeet |
| 600 | Amaan |
输出应该是
| actId | name | LikedBy |
------------------------------------------
| 100 | Amit | Alok, Arjun, Arpeet|
| 500 | Amit | Amaan |
NOTE - 我不希望使用 FOR XML PATH 来实现这一点,因为它不受支持我正在使用的 SQLite。
我的查询尝试是
SELECT a.id, u.name,
(select group_concat(u.name) from activity a, user u where a.id = u.id and a.type = 'like'
group by a.parent) as likedby
FROM activity a, user u
WHERE a.id = u.id and a.type = 'post'
我得到的结果是
| Id | name | LikedBy |
---------------------------------------
| 100 | Amit | Alok, Arjun, Arpeet|
您可以看到结果中缺少另一行。
试试这个:
select t.id4id Id, n1.Name Name, group_concat(n2.name) Groups
from (select t1.actid id4id, t1.postedby id4name, t2.postedby id4groups
from Activity t1 join Activity t2 on t1.actid = t2.parentid) t join "user" n1 on t.id4name = n1.userid join "User" n2 on t.id4groups = n2.userid
group by id4id
注意事项: 1. 我假设 post 在 parent
中总是有一个空值
您不需要子查询。您只需要两个连接即可获取原始名称和喜欢的名称的值:
select a.parentid, u.name, group_concat(ul.name separator ', ') as likedby
from activity a join
user ul
on a.postedby = ul.userid join
user u
on a.parentid = u.userid
where a.type = 'like'
group by a.parentid, u.name;
我有两个表:
1.Activity-- which stores all the activities from a user
2.User-- which stores all the registered users
我想在一个查询中获取所有 post 和所有 post 的类似内容。一行是 post 还是类似由 type 列的值决定。
Table 姓名 - Activity
| actId | parentId | type | postedby |
---------------------------------------------------
| 100 | NULL | post | 100 |
| 200 | 100 | like | 200 |
| 300 | 100 | like | 300 |
| 400 | 100 | like | 400 |
| 500 | NULL | post | 100 |
| 600 | 500 | like | 600 |
Table 姓名 - 用户
| userId | name |
-------------------
| 100 | Amit |
| 200 | Alok |
| 300 | Arjun |
| 400 | Arpeet |
| 600 | Amaan |
输出应该是
| actId | name | LikedBy |
------------------------------------------
| 100 | Amit | Alok, Arjun, Arpeet|
| 500 | Amit | Amaan |
NOTE - 我不希望使用 FOR XML PATH 来实现这一点,因为它不受支持我正在使用的 SQLite。
我的查询尝试是
SELECT a.id, u.name,
(select group_concat(u.name) from activity a, user u where a.id = u.id and a.type = 'like'
group by a.parent) as likedby
FROM activity a, user u
WHERE a.id = u.id and a.type = 'post'
我得到的结果是
| Id | name | LikedBy |
---------------------------------------
| 100 | Amit | Alok, Arjun, Arpeet|
您可以看到结果中缺少另一行。
试试这个:
select t.id4id Id, n1.Name Name, group_concat(n2.name) Groups
from (select t1.actid id4id, t1.postedby id4name, t2.postedby id4groups
from Activity t1 join Activity t2 on t1.actid = t2.parentid) t join "user" n1 on t.id4name = n1.userid join "User" n2 on t.id4groups = n2.userid
group by id4id
注意事项: 1. 我假设 post 在 parent
中总是有一个空值您不需要子查询。您只需要两个连接即可获取原始名称和喜欢的名称的值:
select a.parentid, u.name, group_concat(ul.name separator ', ') as likedby
from activity a join
user ul
on a.postedby = ul.userid join
user u
on a.parentid = u.userid
where a.type = 'like'
group by a.parentid, u.name;