| 1) Do garden 2) Feed cats 3) Paint roof 4) Take dog for walk 5) Relax 6) Feed cats | | GROUP_CONCAT(TaskId, ') ', TaskName SEPARATOR ' ') | You can also concatenate columns, and provide their own separator by providing a string literal value.Įxample: SELECT GROUP_CONCAT(TaskId, ') ', TaskName SEPARATOR ' ') | Do garden + Feed cats + Paint roof + Relax + Take dog for walk | | GROUP_CONCAT(DISTINCT TaskName SEPARATOR ' + ') | To do this, use SEPARATOR followed by the string literal value that should be inserted between group values.Įxample: SELECT GROUP_CONCAT(DISTINCT TaskName SEPARATOR ' + ') However, you can specify a delimiter of your choice if required. Example – Specify a Delimiterīy default, the list is a comma-separated list. The alternative (and default) value is ASC for ascending. So in this case I use DESC to specify that it should be in descending order. | Take dog for walk,Relax,Paint roof,Feed cats,Do garden | | GROUP_CONCAT(DISTINCT TaskName ORDER BY TaskName DESC) | You can use ORDER BY to order the results by a given column.Įxample: SELECT GROUP_CONCAT(DISTINCT TaskName ORDER BY TaskName DESC) So in this case, “Feed cats” is only listed once, whereas it was listed twice in the previous example. | Do garden,Feed cats,Paint roof,Relax,Take dog for walk | You can use DISTINCT to remove duplicates (so that duplicate records become one record).Įxample: SELECT GROUP_CONCAT(DISTINCT TaskName) Note that there are restrictions on how long this list can be. By default, the list is separated by a comma. | Do garden,Feed cats,Paint roof,Take dog for walk,Relax,Feed cats |Īs you can see, each row from the result set has been concatenated into a single row. Here’s a basic example to demonstrate the GROUP_CONCAT() function: SELECT GROUP_CONCAT(TaskName) The Dataįirst, let’s use the following data in our first few examples: USE Solutions This article provides examples of how it all works. The GROUP_CONCAT() function was built specifically for the purpose of concatenating a query’s result set into a list separated by either a comma, or a delimiter of your choice. In MySQL, you can return your query results as a comma separated list by using the GROUP_CONCAT() function.
0 Comments
Leave a Reply. |