也许你可以帮助我:
如果有table1:accounts
user_id account_id
39 3799207
39 80286966
40 3789458
...
table2:订阅者
id client_id master_id master_account_id active
1 43 39 3799207 1
2 43 39 80286966 1
3 44 39 80286966 1
4 45 39 80286966 1
...
使用此请求:
'SELECT account_id FROM accounts WHERE user_id = "39"';
我可以得到这张表:
Account
3799207
80286966
我怎样才能得到这张表:
Account Subscribers Count
3799207 43 1
80286966 43,44,45... 3
谢谢!
答案 0 :(得分:6)
试试这个:
SELECT a.account_id Account,
GROUP_CONCAT(b.client_id) Subscribers,
COUNT(b.client_id) `Count`
FROM accounts a INNER JOIN subscribers b
on a.account_id = b.master_account_id
WHERE b.master_id = '39'
GROUP BY a.account_id
MySQl
和SQLite
内置了函数GROUP_CONCAT
,它将列连接成行。
HTH