且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将匹配逗号分隔的列表与值相匹配?

更新时间:2022-10-14 22:35:05


  1. 将CSV存储在需要查询的列中是一个坏主意 - 您应该使用单独的表格

  2. IN不适用于CSVs - 它用于列出单个列的值

  3. 除了这些参数之外,您可以使用 FIND_IN_SET()

例如:

  SELECT * FROM article WHERE FIND_IN_SET('5',category)!= 0; 


Hello I have a table with articles and the articles have a column category. The categories are filled like 1,4,6,8

How can i check if there is a product which has in it for example category 5

I tried something like

select * from article where category in(5); 

But that doesn't work. If I use like than i will have a problem with for example 1 and 10. So how can I do this in one mysql query?

  1. Storing CSV in a column you need to query is a bad idea - you should use a separate table.
  2. IN is not for CSVs - it's for listing values for a single column
  3. Those arguments aside, you can use FIND_IN_SET()

For example:

SELECT * FROM article WHERE FIND_IN_SET('5', category) != 0;