MySQL - is it possible to use group_concat in an IN() statement?

rotor155

I've been trying to use "group_concat" to build a list of values for use in IN(), but even carefully crafting a comma delimited, single-quoted value string doesn't seem to work. The following does not "find" "fr" in the group_concat'ed list, even though the displayed value for the group_concat is 'de','es','fr', and a result set of one record is incorrectly returned;

SELECT Message, table_id FROM Tenant
LEFT JOIN Translation on Translation.table_id=Tenant.ID
WHERE table_name='Tenant' AND table_column='Message' AND Tenant.ID=2
GROUP BY table_name, table_column, table_id HAVING 'fr' NOT 
IN(group_concat(CONCAT('''', language, '''')));

Whereas below, "fr" IS indeed found and the resultset is appropriately empty:

SELECT Message, table_id FROM Tenant
LEFT JOIN Translation on Translation.table_id=Tenant.ID
WHERE table_name='Tenant' AND table_column='Message' AND Tenant.ID=2
GROUP BY table_name, table_column, table_id HAVING 'fr' NOT IN('de','es','fr')

I realize I can use LOCATE();

SELECT Message, table_id FROM Tenant
LEFT JOIN Translation on Translation.table_id=Tenant.ID
WHERE table_name='Tenant' AND table_column='Message' AND Tenant.ID=2
GROUP BY table_name, table_column, table_id HAVING LOCATE('de', 
group_concat(language)) = 0

But such a text search is likely less efficient compared to "IN()", which is likely scheduled differently by the MySQL optimizer.

I've tried every trick I'm aware of (cast(), trying to reconstitute the list, etc) and I'm fresh out. I'm not sure if this is a limit of group_concat, or perhaps even a "bug" in MySQL compiler. If anyone has any good ideas, I'd be most appreciative.

Thanks

Tim Biegeleisen

You should be using conditional aggregation in your HAVING clause to check for the French language string:

SELECT
    table_id
FROM Tenant
LEFT JOIN Translation
    ON Translation.table_id = Tenant.ID
WHERE
    table_name = 'Tenant' AND
    table_column = 'Message' AND
    Tenant.ID = 2
GROUP BY
    table_id
HAVING SUM(CASE WHEN language = 'fr' THEN 1 ELSE 0 END) = 0

Note that I removed the table_name and table_column from the GROUP BY list, because you specify fixed values for these columns in the WHERE clause. Also, I removed Message from the SELECT list because it does not appear in the group by nor is it inside an aggregate function. Your original query would not even run on certain versions of MySQL or most other databases.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mysql - Possible solutions for "group by group_concat()"?

From Dev

Is it possible to use group_concat with coredata?

From Dev

MySQL Use GROUP_CONCAT with Multiple JOINS

From Dev

MySQL group_concat() ordering by case statement values

From Dev

How to use conditional statement in conjunction with group_concat?

From Dev

Invalid use of group function (group_concat and MySQL)

From Dev

MySQL use GROUP_CONCAT when INSERTing data

From Dev

how to use select query in a Group_concat sub query in mysql

From Dev

MySQL Group_Concat Not In

From Dev

Group_concat use?

From Dev

Group_concat use?

From Dev

Group Concat in mysql statement

From Dev

MySQL: update with join using GROUP_CONCAT: ERROR 1111 (HY000): Invalid use of group function

From Dev

If condition with group_concat in mysql

From Dev

Mysql with GROUP_CONCAT in subselect

From Dev

Mysql GROUP_CONCAT and IN query

From Dev

MySQL group_concat with join

From Dev

MySQL group_concat and count

From Dev

mySQL GROUP_CONCAT - Query

From Dev

MySQL: Nested GROUP_CONCAT

From Dev

Inversing group_concat in Mysql

From Dev

MySQL group_concat problems

From Dev

Reasons not to use GROUP_CONCAT?

From Dev

Use conditions in GROUP_CONCAT

From Dev

MySQL group_concat and nest with another group_concat

From Dev

MySQL group_concat and nest with another group_concat

From Dev

MySQL - GROUP_CONCAT returns duplicate data, can't use DISTINCT

From Dev

How to use GROUP_CONCAT in mySQL when one of the fields contain comma seperated numbers?

From Dev

MySQL - GROUP_CONCAT returns duplicate data, can't use DISTINCT