English 中文(简体)
MYSQL 与COCAT的两张表格提出的MYSQL要求
原标题:MYSQL request from two tables with CONCAT

也许你可以帮助我:

如果有一个表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

谢谢!

最佳回答

尝试此 :

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, HTH, HTH, HTH, HTH, HTH, HTH, HTH, HT, HTH

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签