Merge the multi-row query results in mysql into one
SELECT GROUP_CONCAT (md. data1) from data md, contacts cc WHERE md. conskey = cc. id AND md. mimetype_id = 5 AND md. user_id = 17: when a single ID corresponds to multiple names using the function: group_concat (), the name is originally merged into a row for multiple rows of data, such as | 1 | 10, 20, 20 | this article introduces how to use the group_concat function in MySQL through examples, such as select group_concat (name ). The complete syntax of the group_concat function in MySQL is as follows: group_concat ([DISTINCT] field to be connected [Order by asc/DESC sorting field] [Separator 'delimier']) basic query mysql> select * from aa; + ------ + | id | name | + ------ + | 1 | 10 | 1 | 20 | 1 | 20 | 2 | 20 | 3 | 200 | | 3 | 500 | + ------ + 6 rows in set (0.00 sec) group by id, print the value of the name field in one row, and separate them by commas (,) (default) mysql> select id, group_concat (name) from aa group by id; + ------ + -------------------- + | id | group_concat (name) | + ------ + -------------------- + | 1 | 10, 20, 20 | 2 | 20 | 3 | 200,500 | + ------ + ------------------ + 3 rows in set (0.00 sec) grouped by id, print the value of the name field in one row, use semicolons to separate mysql> select id, group_concat (name separator ';') from aa group by id; + ------ + ---------------------------------- + | id | group_concat (name separator ';') | + ------ + ------------------------------ + | 1 | 10; 20; 20 | 2 | 20 | 3 | 200; 500 | + ------ + ------------------------------------ + 3 rows in set (0.00 sec) group by id and print the value of the redundant name field in one row. Separate them by commas (,) from mysql> select id, group_concat (distinct name) from aa group by id; + ------ + ----------------------------- + | id | group_concat (distinct name) | + ------ + --------------------------- + | 1 | 200,500 | 2 | 20 | 3 | 0.00 | + ------ + --------------------------- + 3 rows in set (sec) grouped by id, print the value of the name field in one row, separated by commas (,), in reverse order of name mysql> select id, group_concat (name order by name desc) from aa group by id; + ------ + ------------------------------------- + | id | group_concat (name order by name desc) | + ------ + --------------------------------------- + | 1 |, 10 | 2 | 20 | 3 | 500,200 | + ------ + --------------------------------------- + 3 rows in set (0.00 sec)