2

I want to select one column values separated by commas from the below table.

username
--------
   A
   B
   C
   D

I want the result like A,B,C,D.

Jkmorayur
  • 29
  • 4
  • See this post. http://stackoverflow.com/questions/180032/how-can-i-combine-multiple-rows-into-a-comma-delimited-list-in-sql-server-2005 – David Oct 17 '12 at 04:25

3 Answers3

5

you can do it directly on your query. use GROUP_CONCAT for this,

SELECT GROUP_CONCAT(`username`)
FROM tableName
John Woo
  • 258,903
  • 69
  • 498
  • 492
1

Use this code:

$qry="select username from tableName ";
$exe=mysql_query($qry);
while($r=mysql_fetch_array($exe))
{
        $userName .=$r['username'].",";
}
$userName =substr($userName,0,-1);
echo $userName;
VijayS91
  • 1,535
  • 23
  • 38
1

Try like this:

$sql = 'SELECT username FROM table_name';
$query = mysql_query($sql);

$csv = array();

while($row = mysql_fetch_array($query)) {
    $csv[] = $row['username'];
}

echo implode(',', $csv);