Group concat is driving me nuts.
I have 1 table 2 columns
FRUIT QUANTITY
APPLE 4
ORANGE 6
I'd like to group concat these into one field
orange:4, apples:6
You can concat() both columns for each row, then group_concat() the results over all rows:
select group_concat(concat(fruit, ':', qty) separator ', ') res from mytable
You might want to consider adding an order by to group_concat() in order to get predictable, consistent results.
You can list multiple columns and other values in the GROUP_CONCAT() call, and it will concatenate them together.
SELECT GROUP_CONCAT(fruit, ':', qty SEPARATOR ', ') AS fruits
FROM yourTable
If you have lots of columns that you're joining like this, you could use CONCAT_WS() to concatenate them all together with the same delimiter. See GROUP_CONCAT multiple columns as an array or exolodable string
Related
Tried to group_concat a text field in mysql.But its taking only the first value.(if the values are '177,178') it taking only 177 because I am saving this as a text field.So how can I do group_concat with this fields?
My query looks as such:
SELECT GROUP_CONCAT(abc.displayValue SEPARATOR ' ') FROM abc WHERE abc.lookupId IN ('177,178')
Are you misplacing the quotes within your IN?
SELECT GROUP_CONCAT(abc.displayValue SEPARATOR ',') FROM abc WHERE abc.lookupId IN (177,178)
The problem isn't with GROUP_CONCAT, the problem is that your WHERE clause is only selecting one row. Since the lookupId field is an integer, it's converting the string '177,178' to an integer, which is just 177. You shouldn't have quotes around the values in the IN() list, that's making it just a single value to look for.
SELECT GROUP_CONCAT(abc.displayValue SEPARATOR ' ')
FROM abc
WHERE abc.lookupId IN (177, 178)
If the comma-separated string is actually coming from a column in a table you're joining with, see sql join tables where 1 column has comma.
SELECT GROUP_CONCAT(table1.displayValue SEPARATOR ' ')
FROM table1
JOIN table2 ON FIND_IN_SET(table1.lookupID, table2.lookupIDs)
WHERE ...
I have a mysql table where I some data are repeated in one column but have different value in another. I want to concat them an them and create new string.
I am getting data in following format:
See rows with checkbox option and vodaphoneissues and comments are repeated twice, yet have different values in another column.
I want to concatenate that value with comma separated format.
There is a function called GROUP_CONCAT.
I do not know names of your columns, let's say they are COLUMN1, COLUMN2, etc. The code will be:
SELECT COLUMN3, GROUP_CONCAT(COLUMN4) FROM your_table GROUP BY COLUMN3;
Use GROUP_CONCAT for that purposes
SELECT id, GROUP_CONCAT(DISTINCT column_name ORDER BY column_name SEPARATOR ', ')
FROM table
GROUP BY comments, versions;
Also pay attention to GROUP BY clouse as it groups by few columns:
GROUP BY comments, versions;
I'm using GROUP_CONCAT to build up Quartiles and am unsure how to get it working on scores created from two columns.
My GROUP_CONCAT looks like this - but at the moment is ordering on 'gst.raw_gps' when it should be ordering on the values arrived at through '(100/gst.max_gps)*gst.raw_gps'
GROUP_CONCAT(
(100/gst.max_gps)*gst.raw_gps
ORDER BY gst.raw_gps
SEPARATOR ','
)
Any advice much appreciated
Then use the expression instead of the column name:
GROUP_CONCAT((100/gst.max_gps)*gst.raw_gps
ORDER BY (100/gst.max_gps)*gst.raw_gps
SEPARATOR ','
)
I am trying to get distinct 'productnumber' ordered by the 'createdate' but at the end I need only "productnumber" sorted by 'createdate' (I cannot use top as the 'productnumber' varies in a wide flexible range).
The distinct product number is needed to be concatenated with comma (,) for e.g.
123,245,787,875 (each number represents a productnumber)
The required query looks like somewhat (this query doesn't work and gives an error The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified):
SELECT stuff(
(
SELECT distinct ',' + productnumber
FROM Product
ORDER BY createdate
FOR XML path('')
)), 1, 1, '')
Could anyone help in how to go about to get the desired result.
As Mikael Eriksson suggested, using Group By clause solved the problem. Thanks Mikael
SELECT stuff((SELECT ',' + productnumber
FROM product a
ORDER BY a.createdate FOR xml path('')),1,1,'')
AS destn_str
I'm probably having a no-brain moment.
I want to return a series of numbers using GROUP_CONCAT from two fields in my database. I have done this so far using the following:
SELECT t_id,
CONCAT(GROUP_CONCAT(DISTINCT s_id),',',IFNULL(GROUP_CONCAT(DISTINCT i_id),'')) AS all_ids
FROM mytable GROUP BY t_id
This works fine but if i_id is NULL then of course I get an unnecessary comma. Is there a better way to do this so I don't end up with a comma at the end if i_id is NULL?
You need to use CONCAT_WS to avoid extra comma for NULL values, try this:
SELECT t_id,
CONCAT_WS(',', GROUP_CONCAT(DISTINCT s_id),
GROUP_CONCAT(DISTINCT i_id)) AS all_ids
FROM mytable
GROUP BY t_id;