我有一个表,其中有一列名为
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
一样吐出。
你可以这样测试:
```
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 |