本文介绍了如何将匹配逗号分隔的列表与值相匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个表的文章,文章有一个列类别。类别填充如1,4,6,8



我如何检查是否有产品在其中有例如5类



我尝试过一些类似

  select * from(5)中的类别) 

但这不行。如果我使用像我会有一个问题,例如1和10.所以我怎么可以在一个mysql查询?

解决方案

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

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

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

例如:

  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;

这篇关于如何将匹配逗号分隔的列表与值相匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 06:19
查看更多