Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL SELECT everything into one big string (columns and rows)

Tags:

sql

mysql

I have a big MySQL SELECT query, that I need to convert into one string only, so I can make an UPDATE out of it, without having to use two separate querys. For the sake of simplicity, let's assume we have a SELECT query that returns this result:

enter image description here

How do I convert all of it into something like this :

1,Bob,20;2,Adam,30;3,Steve,40;

That I can use to UPDATE some other table with ?

Knowing that both the number of columns and rows can change and are not static. (very important ! Especially the columns!). How can I pull this off ? I don't think CONCAT() can help in this situation.

Any help would be appreciated. Thanky ou.

like image 783
Reacen Avatar asked Feb 20 '13 05:02

Reacen


People also ask

How do I combine multiple rows into one in MySQL?

MySQL | Group_CONCAT() Function. The GROUP_CONCAT() function in MySQL is used to concatenate data from multiple rows into one field. This is an aggregate (GROUP BY) function which returns a String value, if the group contains at least one non-NULL value. Otherwise, it returns NULL.

How do I select multiple values from one table in MySQL?

Learn MySQL from scratch for Data Science and Analytics To select multiple values, you can use where clause with OR and IN operator.

How do I select an entire table in MySQL?

We use the SELECT * FROM table_name command to select all the columns of a given table. In the following example we are selecting all the columns of the employee table. mysql> SELECT * FROM employee; And we get the following output.

Does MySQL support PIVOT function?

Unfortunately, MySQL does not have PIVOT function, so in order to rotate data from rows into columns you will have to use a CASE expression along with an aggregate function.


1 Answers

Can you try this?

SELECT group_concat(concat(id, ',', name, ',', age) separator ';')
FROM test

http://www.sqlfiddle.com/#!2/915557/9

like image 104
Lloyd Santos Avatar answered Oct 05 '22 04:10

Lloyd Santos