使用CONCAT从两个表请求MYSQL

使用CONCAT从两个表请求MYSQL,mysql,sql,request,database-table,Mysql,Sql,Request,Database Table,也许你能帮我: 如果有表1:账目 user_id account_id 39 3799207 39 80286966 40 3789458 表2:订户 id client_id master_id master_account_id active 1 43 39 3799207 1 2 43 39 80286966

也许你能帮我:

如果有表1:账目

user_id   account_id   
39        3799207
39        80286966
40        3789458

表2:订户

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
我怎样才能得到这张桌子:

Account
3799207
80286966
Account      Subscribers     Count
3799207      43              1
80286966     43,44,45...     3
谢谢

试试这个:

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\u CONCAT
,可将列合并成行

HTH

试试这个:

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\u CONCAT
,可将列合并成行