mraiur
mraiur

Reputation: 136

mysql get many id's on one row

How to get all ids separated by comma on one row.

id | name

1 | Jonny
2 | Lisa
3 | Ben

And with php/mysql get with one query without a loop the ids comma separated. Like "1,2,3"

To be something like this :

$query = "SELECT 1,2,3 as oneRowIds FROM tableName";
$result = mysql_query($query);
$result = mysql_fetch_assoc($result);
echo $result['oneRowIds'];// and that shows "1,2,3"

Upvotes: 2

Views: 2735

Answers (1)

user319198
user319198

Reputation:

use group_concat function

GROUP_CONCAT() function is used to concatenate column values into a single string. It is very useful if you would otherwise perform a lookup of many row and then concatenate them on the client end.

select group_concat(id) as oneRowIds FROM tableName

Upvotes: 9

Related Questions