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:
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.
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.
Learn MySQL from scratch for Data Science and Analytics To select multiple values, you can use where clause with OR and IN operator.
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.
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.
Can you try this?
SELECT group_concat(concat(id, ',', name, ',', age) separator ';')
FROM test
http://www.sqlfiddle.com/#!2/915557/9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With