I have column A in the Sample Table1. Column A has values as follows.
+----+
| A |
+----+
| a1 |
| a2 |
| a3 |
| a4 |
| a5 |
+----+
I need a query that should give the following output. All the Values should be
"a1","a2","a3","a4","a5"
Is there a way?
Use the CONCATENATE function: Use the CONCATENATE function in column D: =CONCATENATE(A1,B1,C1). In the menu bar, select Insert, Function. Click Text functions and select CONCATENATE.
All you have to do is, declare a varchar variable and inside the coalesce, concat the variable with comma and the column, then assign the COALESCE to the variable.
The GROUP_CONCAT() function in MySQL is used to concatenate data from multiple rows into one field. This is an aggregate (GROUP BY) function which returns a String value, if the group contains at least one non-NULL value. Otherwise, it returns NULL.
CONCAT() function in MySQL is used to concatenating the given arguments. It may have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string.
You could use a combination of CONCAT
and GROUP_CONCAT
Query
SELECT GROUP_CONCAT(CONCAT('"', A, '"')) AS `combined_A`
FROM `your_table_name`;
And if you want to remove the duplicates. Then use DISTINCT
with GROUP_CONCAT
.
Query
SELECT GROUP_CONCAT(DISTINCT CONCAT('"', `A`, '"')) AS `combined_A`
FROM `your_table_name`;
SQL Fiddle demo
Use GROUP_CONCAT() function to achive this.
SELECT GROUP_CONCAT(<Type your column name here> SEPARATOR ', ') FROM <Table Name>;
Query for your provided sample example :
SELECT GROUP_CONCAT(A SEPARATOR ', ') FROM Table1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With