简体   繁体   English

我想 select 行在列中具有特定值但列可以包含多个值

[英]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.

相关问题 如何选择具有相同值的所有列值的行? - How can I select rows with all Column value, which have the same values? 更新另一列具有多个值的行上的列 - Update a column on rows of which another column has multiple values 我怎样才能 select 多行与同一个表中的其他行的计数在一个单独的列中具有它们的主键? - How can I select multiple rows with the count of other rows in the same table that has their primary keys in a separate column? 当具有col val1的多行时,如何从不包含列值2中特定值的表中获得不同的列value1? - how to get distinct column value1 from a table which does not include a particular value in column value 2 when it has multiple rows with col val1? 如何将多个值添加到 SQL 中已填充其他行的列 - How do I add multiple values to a column in SQL which already has other rows filled 我怎样才能 select 所有不与另一行 null 共享列值的行? - How can I select all the rows which do not share a column value with another row which is null? 如何在mysql数据库的特定列中选择最后一个值? - How can i select the last value in a particular column in mysql database? Select 不同的列,其中另一列不包含特定值 - Select distinct columns where another column does not contain a particular value SELECT 来自具有多个数组值的列的值 - SELECT a value from a column that has multiple array values mysql:如果其中一行在 B 列中具有特定值,我如何忽略包含(值列 A)的所有行? - mysql: how do I ignore all rows that contain (value column A), if one of these rows has a specific value in column B?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM