Hi,
I'm looking to pull data (just one column) from mysql which is over multiple rows.
e.g.
apple
banana
orange
pear
Now, I actually want the data in this format in just one row:
"apple,banana,orange,pear"
Does mysql offer some sort of "SELECT IMPLODE(',', my_column) AS foo" type of thing? I know I can do it with PHP but this a part of a much larger query so I'm trying to keep this all in a single query.
Cheers,
d11
Equivalent of implode() purely in MySQL
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
I tried but it just sticks it with the column from one row only. I then tried:Pimptastic wrote:CONCAT_WS() maybe?
Code: Select all
select concat_ws(', ', (select column_name from tbl_name)) as joined_string
I've approached the problem from a different angle anyway and still got it in one query but I'll need to use PHP to implode thos columns.
If anybody happens to know the MySQL way to do it, it would be cool though, although I won't use it in this instance now.
Where's ~timvw?
Im just throwing things at you here now but what does GROUP_CONCAT do?
http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html
http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Maybe
would work for you when saving it, and unserialize() when pulling it back out.
Code: Select all
serialize()