如何检查字符串 MySQL 中是否存在逗号分隔字符串的值

问题描述 投票:0回答:1

我有一个表,其中有一列名为

tags
。在本专栏中,我有一个以逗号分隔的列表,例如
air conditioner, tv, fridge

我想检查逗号分隔列中的任何单词是否存在于类似的字符串中

Is there an air conditioner in the room?

因此,查询应返回与

tags
列中的匹配项,
air conditioner
值存在。

我正在考虑使用

find_in_set
,但在这种情况下它不起作用,因为逻辑应该颠倒。

select * from products where find_in_set('Is there an air conditioner in the room?',tags) <> 0

更新

我可以通过将字符串分成单词然后检查标签列来使用

find_in_set
,但它不适用于像“空调”这样的组合,因为它会像
air, conditioner
一样吐出。

mysql find-in-set
1个回答
0
投票

你可以这样测试:

``` 
create table mytags (
  id int(11),
  tags varchar(64)
  );

insert into mytags values(1,"air conditioner, tv, fridge");

SELECT * from mytags;


```


| id | tags |
|---:|:-----|
| 1 | air conditioner, tv, fridge |

``` 
SELECT * FROM mytags WHERE FIND_IN_SET("air conditioner",tags);
```
| id | tags |
|---:|:-----|
| 1 | air conditioner, tv, fridge |

小提琴

© www.soinside.com 2019 - 2024. All rights reserved.