1

I have database table :

+----+--------+-----------+
| id |  name  | know_from |
+----+--------+-----------+
|  1 | Andy   |           |
|  2 | Tony   | Andy      |
|  3 | Ben    | Andy      |
|  4 | Miller | Ben       |
|  5 | Bob    | Tony      |
|  6 | Scott  | Andy      |
+----+--------+-----------+

How to effectively query it and expecting the result as below :

+----+--------+-------+------------------+
| id |  name  | total |  reference_list  |
+----+--------+-------+------------------+
|  1 | Andy   |     3 | Tony, Ben, Scott |
|  2 | Tony   |     1 | Bob              |
|  3 | Ben    |     1 | Miller           |
|  4 | Miller |     0 |                  |
|  5 | Bob    |     0 |                  |
|  6 | Scott  |     0 |                  |
+----+--------+-------+------------------+

I have a solution using GROUP_CONCAT( know_from SEPARATOR ',') but the server is still using MySQL ver 5.1 which doesn't support this syntax. Any query alternative? Thanks in advance!

P.S.) Don't tell me to update the version as solution

4
  • You might want to post your whole query solution to make it clearer on where is the problem Commented Jul 22, 2019 at 4:22
  • 1
    As far as I can see, MySQL 5.1. does support that syntax. Unless you include the comma. Then AFAIK no version supports it. i.e. GROUP_CONCAT(name SEPARATOR ',') should work, GROUP_CONCAT(name, SEPARATOR ',') can't possibly. Commented Jul 22, 2019 at 4:28
  • Thanks, I think 5.1 just doesn't work with SEPARATOR Commented Jul 22, 2019 at 5:21
  • 1
    Docs say it does. Then again, I can't test it - I'm certainly not going to install 5.1. :P Commented Jul 22, 2019 at 6:01

2 Answers 2

2

try concating the name string with the separator before the group concat

group_concat(concat(name,','))

And remove the last , from the resultant string.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I use GROUP_CONCAT(know_from) instead without SEPARATOR
0

try this:

group_concat(`name` separator ',') as Name

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.