Home > Database > Mysql Tutorial > How Can I Replicate Oracle's LISTAGG() Function in MySQL?

How Can I Replicate Oracle's LISTAGG() Function in MySQL?

Susan Sarandon
Release: 2024-12-08 21:46:12
Original
860 people have browsed it

How Can I Replicate Oracle's LISTAGG() Function in MySQL?

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;
Copy after login

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
Copy after login

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;
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template