简体   繁体   中英

SQL query to fetch rows where a value is present in a comma separated field

what is the Query For -> How To Get The table content whose column consists of data comma sepreted value how to check is that value is present in that coloumn if present then select that ID of product

id   productname  poduct_cat_id
1    abc          2,3,4
2    def          3,4
3    efg          1,2,5

How to check 3 in product_cat_id and get the product name and id

output: result[id]=1 result[productname]=abc

I agree with Phil's comment above.

For you table you can use FIND_IN_SET function -

SELECT * FROM table WHERE FIND_IN_SET(3, product_cat_id);

If you add a leading and trailing comma to the field, then you can use a straightforward like expression to select the rows of interest.

SELECT * FROM table
WHERE CONCAT(',', product_cat_id, ',') LIKE '%,3,%'

我估计FIND_IN_SET函数将是你的朋友。

select id, productname
from table
where product_cat_id like '%,3,%'
or product_cat_id like '3,%'
or product_cat_id like '%,3'

If you don't consider changing the structure of the table... sight:

This is a lame query:

SELECT `id`,`productname` 
FROM `place_table_name_here` 
WHERE `poduct_cat_id` LIKE '%3%';

but it will give you the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM