Delos Chang
Delos Chang

Reputation: 1853

MySQL column data returned as comma delimited list

I currently have a MySQL table like:

id | friend
1  |   2
1  |   5
1  |   10
3  |   6
15 |   19
21 |   4

I'm trying to grab all the friend id's of one particular user and arrange them into a comma-delimited list. For example, grabbed user1's friends, it would return as

$friend_list = 2,5,10

Currently, I have:

$sql = "SELECT friend FROM table__friends WHERE id = ".$user_id;

This only grabs one row though..please help!

Thanks

Upvotes: 5

Views: 25838

Answers (5)

GDP
GDP

Reputation: 8178

You want to use GROUP_CONCAT :

$sql = "SELECT GROUP_CONCAT(friend) FROM table__friends GROUP BY id HAVING id = ".$user_id;

Adjusted for correctness per the better answer.

Upvotes: 13

We Are All Monica
We Are All Monica

Reputation: 13336

Your query will return more than one row - it sounds like you're only reading the first row. You want something like this instead:

$sql = "SELECT friend FROM table__friends WHERE id = "
  . mysql_real_escape_string($user_id);

$result = mysql_query($sql);
if (!$result) {
  die("Something bad happened");
}

$friend_arr = array();
while ($row = mysql_fetch_array($result)) {
  $friend_arr[] = $row[0];
}

$friend_list = implode(',', $friend_arr);

Post the PHP code you're using to run the query and we'll be able to help more.

A couple of notes:

  • I added the mysql_real_escape_string function. Sanitizing your user inputs in this way is crucial to avoid SQL injection attacks.
  • Depending on what you're actually doing - there's a good chance that storing a comma-separated list of strings isn't a good way to do it.

Upvotes: 1

jcho360
jcho360

Reputation: 3759

I used a Variable called @a, to store the ids, at the end you have the values in the variable or try with a limit, group order by, like this:

mysql> show create table actor\G
*************************** 1. row ***************************
       Table: actor
Create Table: CREATE TABLE `actor` (
  `actor_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `first_name` varchar(45) NOT NULL,
  `last_name` varchar(45) NOT NULL,
  `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`actor_id`),
  KEY `idx_actor_last_name` (`last_name`)
) ENGINE=InnoDB AUTO_INCREMENT=207 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)


mysql> set @a:=0;
Query OK, 0 rows affected (0.00 sec)

mysql> select @a:=concat(@a,",",actor_id) as actor_id from actor where actor_id>195 order by (actor_id) desc limit 1;
+----------------------------+
| actor_id                   |
+----------------------------+
| 0,196,197,198,199,200,205, |
+----------------------------+
1 row in set (0.00 sec)

In your case change the "WHERE condition"

or you can also after the select:

mysql> select @a;
+-------------------------------+
| @a                            |
+-------------------------------+
| 0,196,197,198,199,200,205,206 |
+-------------------------------+
1 row in set (0.00 sec)

Upvotes: 0

Andrius Naruševičius
Andrius Naruševičius

Reputation: 8578

You are probably looking for GROUP_CONCAT(column) function. Examples here and here.

Upvotes: 0

WojtekT
WojtekT

Reputation: 4775

$sql = "SELECT GROUP_CONCAT (DISTINCT friend SEPARATOR ',') 
      FROM table_friends GROUP BY id 
       HAVING id =".$user_id; 

Upvotes: 4

Related Questions