MySQL Aggregate Function: List
In MySQL, the LISTAGG() function used in Oracle is not available. However, you can employ the GROUP_CONCAT() function to achieve a similar functionality. This aggregate function combines values from multiple rows into a single string.
To replicate the behavior of Oracle's LISTAGG() function, you can use GROUP_CONCAT() as follows:
SELECT GROUP_CONCAT(MyString SEPARATOR ', ') AS myList FROM table WHERE id < 4;
This query will concatenate the values in the MyString column for all rows where the Id column is less than 4, and separate them with commas. The result will be a single string containing the values:
First, Second, Third
You can optionally group the results by different columns to create a hierarchical list. For instance, to group the results by a parent column, you can use:
SELECT GROUP_CONCAT(MyString SEPARATOR ', ') AS myList FROM table GROUP BY parent_column;
This will generate a list of strings for each unique value in the parent_column.
The above is the detailed content of How Can I Replicate Oracle's LISTAGG() Function in MySQL?. For more information, please follow other related articles on the PHP Chinese website!