用于获取逗号分隔字段中存在值的行的SQL查询

时间:2020-12-22 00:20:23

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

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

如何检查product_cat_id中的3并获取产品名称和ID

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

输出:result [id] = 1 result [productname] = abc

5 个解决方案

#1


2  

I agree with Phil's comment above.

我同意菲尔上面的评论。

For you table you can use FIND_IN_SET function -

对于你的表,你可以使用FIND_IN_SET函数 -

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

#2


1  

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,%'

#3


1  

The FIND_IN_SET function will be your friend here I reckon.

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

#4


0  

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

#5


-1  

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.

但它会给你答案。

#1


2  

I agree with Phil's comment above.

我同意菲尔上面的评论。

For you table you can use FIND_IN_SET function -

对于你的表,你可以使用FIND_IN_SET函数 -

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

#2


1  

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,%'

#3


1  

The FIND_IN_SET function will be your friend here I reckon.

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

#4


0  

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

#5


-1  

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.

但它会给你答案。