[英]I want to select rows which has a particular value in the column but the column can contain multiple values
col_1 col_2
0 ab,bc,cd
1 bc,xy
2 zz,xx
3 ab
4 cc
5 ef,kk,ok
I want to select rows that have "ab" as one of the values in col_2.我想 select 行具有“ab”作为 col_2 中的值之一。 For example - in this case, 0th and 3rd row will be selected.例如 - 在这种情况下,将选择第 0 行和第 3 行。 So, is there any SQL query for that?那么,是否有任何 SQL 查询?
First, you should fix your data model.首先,您应该修复您的数据 model。 Storing multiple values in a string is just a misuse of strings.在一个字符串中存储多个值只是对字符串的滥用。 The correct data model would have a separate row for each col_1
/ col_2
combination.对于每个col_1
/ col_2
组合,正确的数据 model 将有一个单独的行。
Sometimes, we are stuck with other people's really bad decisions on data modeling.有时,我们会被其他人在数据建模方面非常糟糕的决定所困扰。 MySQL actually has a function to help deal with this, find_in_set()
. MySQL 实际上有一个 function 来帮助处理这个问题, find_in_set()
。
You can use:您可以使用:
where find_in_set('ab', col_2) > 0
until you fix the data model.直到您修复数据 model。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.