1

maybe you can help me:

If have a table1: accounts

user_id   account_id   
39        3799207
39        80286966
40        3789458

...

table2: subscribers

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

...

Using this request:

'SELECT account_id FROM accounts WHERE user_id = "39"';

I can get this table:

Account
3799207
80286966

How can I get this table:

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

Thanks!

Raidri
  • 17,258
  • 9
  • 62
  • 65
XTRUST.ORG
  • 3,280
  • 4
  • 34
  • 60

1 Answers1

6

Try this:

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 and SQLite has builtin function GROUP_CONCAT which concactenates columns into rows.

HTH

John Woo
  • 258,903
  • 69
  • 498
  • 492