This article will show you how to use SQL to compress multiple data into one.
The following table data is used as an example:
Depending on your needs, the common scenarios are as follows:
Using a separator for string mosaic is similar to this, you can use the following statement:
SELECT class, wm_concat(',', name) FROM students GROUP BY class;
Another common requirement is as follows:
You can use the following statement for the need to change multiple columns like this:
SELECT class ,SUM(CASE WHEN gender = 'M' THEN 1 ELSE 0 END) AS cnt_m ,SUM(CASE WHEN gender = 'F' THEN 1 ELSE 0 END) AS cnt_f FROM students GROUP BY class;