IN query is not working group_concat in mysql subquery [duplicate] - mysql

This question already has answers here:
MySQL query finding values in a comma separated string
(11 answers)
Closed 5 years ago.
IN query is not working in sub_query of mysql
Please take look-:
SELECT (SELECT GROUP_CONCAT( text )
FROM course_intersted_in_list
WHERE id IN ( urd.interested_course )) as interested_course_text,
urd.*
from user_registration_data as urd
Here urd.interested_course have values like 1,2,3,4 for some users
user_registration_data table is like
id interested_course
1 1,2,3,4,5
2 1,4,5
course_intersted_in_list table is like
id interested_course
1 mbbs
2 dental
3 basic

Since the interested_course has comma separated ids, you can use find_in_set to search a value in csv string:
SELECT (SELECT GROUP_CONCAT( text )
FROM course_intersted_in_list
WHERE find_in_set(id, urd.interested_course)
) as interested_course_text,
urd.*
from user_registration_data urd;
Also, I recommend staying away from CSV data in RDBMS and normalize the data. that way you'll get a system that scales.

You can use MySQL FIND_IN_SET instead:
SELECT (SELECT GROUP_CONCAT( text )
FROM course_intersted_in_list
WHERE find_in_set(id, urd.interested_course)) as interested_course_text,
urd.*
from user_registration_data as urd
IN() doesn't work on table columns, only against a list inside ()

Related

How to extract all different tags from one column? [duplicate]

This question already has answers here:
MySQL - select distinct values from a column where values are separated by comma
(3 answers)
Closed last month.
Considering the data-table as below:
tags
alpha
alpha, beta
delta
beta, gamma
How can get all different tags in one query? So the expected result should be:
alpha
beta
gamma
delta
(order is not important)
I have been thinking a lot, but could not find out the solution. :)
We get all tags splited by ',' then we use union to keeps unique records only :
select TRIM(SUBSTRING_INDEX(tags, ',', 1))
from tags
union
select TRIM(SUBSTRING_INDEX(tags, ',', -1))
from tags
Assuming that there would be a max of 3 CSV words per record and that you are using MySQL 8+, we can try using a regex substring approach:
SELECT DISTINCT tags
FROM
(
SELECT REGEXP_SUBSTR(tags, '\\w+', 1, 1) AS tags FROM yourTable
UNION ALL
SELECT REGEXP_SUBSTR(tags, '\\w+', 1, 2) FROM yourTable
UNION ALL
SELECT REGEXP_SUBSTR(tags, '\\w+', 1, 3) FROM yourTable
) t;

How to search for rows that are a substring of a value in MySQL? [duplicate]

This question already has answers here:
MySQL SELECT query string matching
(3 answers)
Closed 3 years ago.
I have a full length item code that I am looking for, '34FDE353UE2' and a table full of a shortened version of said item codes, like this
itemCode
============
34FDE
35DCF
34FPE
....
How can I look through my rows to see if any of them match the '34FDE353UE2'? In my example, I would hope to get back the first row since 34FDE is a substring of '34FDE353UE2'. How can I write a query to do this?
Using MySQL 5.6.
With like:
select * from tablename
where '34FDE353UE2' like concat(itemCode, '%')
this will return rows where itemCode's value is the starting chars of '34FDE353UE2'.
If you want rows where itemcode is a substring of '34FDE353UE2' then:
select * from tablename
where '34FDE353UE2' like concat('%', itemCode, '%')
this will work:
select * from tablename where REGEXP_SUBSTR(itemCode,'34FDE353UE2')

MySQL Group by value in comma separated column [duplicate]

This question already has an answer here:
Search with comma-separated value mysql
(1 answer)
Closed 4 years ago.
I could use help with the database query I am trying to pull off. I have a table that has a word or phrase, a product id, a language id, and a version id. The version_id is a comma separated column, since the same word can be used in multiple versions and I tried to avoid adding another row with same data except for a different version.
The table currently looks like this
I am trying to get the COUNT of word per product, version, and language. The problem I am getting is seperating the version_id since it is a comma seperated column. Is there a way to get the count for all words in version 1, and version 2, separately?
I currently have this query, which works, but it does not separate by versions in a comma separated column.
SELECT COUNT( translation ) AS translations, fdp.name AS product, fdv.name AS version, fdl.name AS language, fdl.position
FROM `fdnfortidictionary_dictionary` AS `fdd`
LEFT JOIN `fdnfortidictionary_product` AS `fdp` ON fdd.product_id = fdp.id
LEFT JOIN `fdnfortidictionary_version` AS `fdv` ON fdd.version_id = fdv.id
LEFT JOIN `fdnfortidictionary_language` AS `fdl` ON fdd.language_id = fdl.id WHERE fdp.enable = 1
AND fdd.product_id IN (1,2)
AND CONCAT( ",", fdd.version_id, "," ) REGEXP ",(1|2|3|4|5),"
GROUP BY `language`,`version`,`product`,`position`
ORDER BY fdl.position ASC
The Result I am looking for is something like,
It seems like you might have some luck with FIND_IN_SET()
Probably, something like FIND_IN_SET(fdd.version_id, fdl.version_id) > 0

Using a string of a SELECT query to put in on a IN () condition [duplicate]

This question already has answers here:
FIND_IN_SET() vs IN()
(4 answers)
Closed 5 years ago.
How can I use an string input as 1,2,3,4 taken from a SELECT to put in on an IN condition?
I mean, having this output from this:
SELECT id_list
FROM ids
where categories = 4;
id_list is a string like 1,2,3,4.
Now I want to use that string to put in inside a IN ():
SELECT category_name
FROM categories
WHERE categories IN (
SELECT id_list
FROM ids
where categories = 4
)
But this only outputs the result of the first value of the string, in this case, 1.
I know it's not the best implementation, but I'm not the one who made the DB design and I need to make the query this way. This is just a simplified example.
Can someone tell me how con I archieve my goal? Thanks in advance!
You can use FIND_IN_SET
Query
SELECT `category_name`
FROM `categories`
WHERE FIND_IN_SET(`id`, (SELECT `id_list` FROM `ids` WHERE `categories` = 4)) > 0;
Find a fiddle demo here

Check if value exists in a comma separated list [duplicate]

This question already has answers here:
MySQL query finding values in a comma separated string
(11 answers)
Closed 5 years ago.
I have following values in my sql column:-
a,b,c,d e,f
I want to check if b is present in the column.
You can use FIND_IN_SET():
FIND_IN_SET('b',yourcolumn) > 0
As an example to be used in a query:
SELECT * FROM yourtable WHERE FIND_IN_SET('b',yourcolumn) > 0;
you can use FIND_IN_SET()
FIND_IN_SET('a','a,b,c,d,e');
http://www.w3resource.com/mysql/string-functions/mysql-find_in_set-function.php
You can use a like clause on the column, for eg, if the column name contains these values in the users table you can use this query
select * from users where name like "%b%";
Try this:
select * from users where name like "%,b,%" OR name like "b,%" OR name like "%,b" ;