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;
Related
I am quite the novice at MYSQL and would appreciate any pointers - the goal here would be to automate a categorical field using GROUP_CONCAT in a certain way, and then summarize certain patterns in the GROUP_CONCAT field in a new_column. Furthermore, is it possible to add the new_column to the original table in one query? Below is what I've tried and errors to an unknown column "Codes" if this assists:
SELECT
`ID`,
`Code`,
GROUP_CONCAT(DISTINCT `Code` ORDER BY `Code` ASC SEPARATOR ", ") AS `Codes`,
IF(`Codes` LIKE '123%', 'Description1',
IF(`Codes` = '123, R321', 'Description2',
"Logic Needed"))
FROM Table1
GROUP BY `ID`
Instead of nested if statements, I would like to have a CASE statement as a substitute. Reason being is that I already have around 1000 lines of logical already written as "If [column] = "?" Then "?" else if" etc. I feel like using CASE would be an easier transition with the logic. Maybe something like:
SELECT
`ID`,
`Code`,
GROUP_CONCAT(DISTINCT `Code` ORDER BY `Code` ASC SEPARATOR ", ") AS `Codes`,
CASE
WHEN `Codes` LIKE '123%' THEN 'Description1'
WHEN `Codes` = '123, R321' THEN 'Description2'
ELSE "Logic Needed"
END
FROM Table1
GROUP BY `ID`
Table Example:
ID,Code
1,R321
1,123
2,1234
3,1231
4,123
4,R321
Completed Table:
ID,Codes,New_Column
1,"123, R321",Description2
2,1234,Description1
3,1231,Description1
4,"123, R321",Description2
How then can I add back the summarized data to the original table?
Final Table:
ID,Code,New_Column
1,R321,Description2
1,123,Description2
2,1234,Description1
3,1231,Description1
4,123,Description2
4,R321,Description2
Thanks.
You can't refer to a column alias in the same query. You need to do the GROUP_CONCAT() in a subquery, then the main query can refer to Codes to summarize it.
It also doesn't make sense to select Code, since there isn't a single Code value in the group.
SELECT ID, Codes,
CASE
WHEN `Codes` = '123, R321' THEN 'Description2'
WHEN `Codes` LIKE '123%' THEN 'Description1'
ELSE "Logic Needed"
END AS New_Column
FROM (
SELECT
`ID`,
GROUP_CONCAT(DISTINCT `Code` ORDER BY `Code` ASC SEPARATOR ", ") AS `Codes`
FROM Table1
GROUP BY ID
) AS x
As mentioned in a comment, the WHEN clauses are tested in order, so you need to put the more specific cases first. You might want to use FIND_IN_SET() rather than LIKE, since 123% will match 1234, not just 123, something
I need to use group_concat to build a list of comma separated values but I need the values to be quoted inside single quote. How to do this? The query which I have written doesn't work for me.
I have values inside column like this:
userid (column)
1) 1,2
2) 3,4
Query 1:
SELECT GROUP_CONCAT( DISTINCT CONCAT('\'', user_id, '\'') ) as listed_id
Query 2:
SELECT GROUP_CONCAT( DISTINCT CONCAT('''', user_id, '''') ) as listed_id
Expected output:
'1','2','3','4'
But I am getting values like this
'1,2,3,4'
Try this, Its is working perfectly in my case:
SELECT GROUP_CONCAT( DISTINCT CONCAT("'", REPLACE(user_id, "," , "','") , "'")) as listed_id FROM users
Here is the output:
Let's assume I have a table which store register user data, the records might have same registered name but different email, like following:
I want to create a front view to manipulate those data but I don't want those same name show repeatedly, can mysql statement query to output result like
this is the result so far I can do but it can't bind same name into one.
select * from `register`
where `fullname` in (
select `fullname` from `register`
group by `fullname` having count(*) > 1
)
One thing you could do is to do a SELECT DISTINCT on the duplicate row, and make use of the GROUP_CONCAT(); function in MYSQL to concatenate your desired values into one row, and GROUP BY fullname to get the order you wanted.
Note that I am also putting the user ids into a grouped row, so that you can track which ids belong to which name.
SELECT
DISTINCT fullname as full_name,
GROUP_CONCAT(id SEPARATOR ', ') as user_ids,
GROUP_CONCAT(email SEPARATOR ', ') as emails
FROM
tbl_register
GROUP BY
tbl_register.fullname
Working SQL Fiddle
This would be the logical way to do it. Hope this helped. :)
More information on the GROUP_CONCAT(); function here: https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat
Try this:
SELECT DISTINCT *duplicate_column* FROM *table_name1* WHERE *col_id* IN (SELECT *cols_to_dusplay* FROM *table_name1* GROUP_BY *duplicate_column*
I want to remove a spacial character in my query can anyone help. This is my query
select sum(value) from table_1 where id in (1, 2,);
This 1,2, is fetch from other table using sub-query.
To remove the trailing colon, you can use trim():
SELECT TRIM(TRAILING ',' FROM '1,2,');
My guess is that you want to look for individual values in the list, especially because ids don't usually contain commas.
For that, you can do:
select sum(value)
from table_1
where find_in_set(id, '1, 2,') > 0;
If the values are coming from a subquery, you would be better off using the subquery directly (in most cases). The query would be something like:
select sum(value)
from table_1
where id in (<subquery>);
You would need to modify the subquery to return a list of ids, rather than all concatenated into one field.
I'm becoming mad trying to use an "array" of values obtained from a GROUP_CONCAT into a WHERE IN statement, when GROUP_CONCAT only takes one "id" it works ok but when it takes more it doesn't.
As it follows:
START TRANSACTION;
DECLARE #coupon_ids VARCHAR(MAX);
-- Take one or more ids
SET #coupon_ids:=(SELECT IFNULL( (SELECT GROUP_CONCAT(coupon_id) FROM some_table WHERE order_id=(SELECT entity_id FROM sales_order WHERE increment_id=310033638) GROUP BY order_id), (SELECT coupon_id FROM some_table WHERE coupon_id=310033638)));
SELECT #coupon_ids;
INSERT INTO some_table_gift VALUES (NULL,TRIM('whatever'),'','');
SET #lastid:=LAST_INSERT_ID();
-- Here if #coupon_ids is just one id, like 123 it works, if it is a list of them like 123,234,254 it doesn't works
UPDATE some_table SET owner_id=#lastid,is_gift=1 WHERE coupon_id IN (#coupon_ids);
COMMIT;
-- Same here
SELECT coupon_id,owner_id,is_gift FROM some_table WHERE coupon_id IN (#coupon_ids);
Does anyone know how to work with this?
Thanks!
What's your filed type for coupon_id, if it is not any number type than it will not work.
One way you can add quote (single quote) for each result in GROUP_CONCAT Write
GROUP_CONCAT(coupon_id SEPARATOR '","')
remove SELECT #coupon_ids
And
in QUERY try this WHERE coupon_id IN ("#coupon_ids")