我整天都在寻找解决方案,但也许我不知道该怎么问,假设我有2张桌子:
表1:车辆
id ----车辆
1 -----汽车
2 -----厢式货车
3 -----摩托车
表2:附件
id ----名称------类型
1 -----座位----- 1; 2
2 -----轴------ 3
3 -----轮胎----- 1; 2; 3
我需要知道哪种配件适合汽车旅行,所以我需要这样的输出:
1 -----座位
3 -----轮胎
如何使用MySQL查询做到这一点?
谢谢。
最佳答案
考虑到您知道car
的id
是1
,并且type
是string
,可以执行以下操作:
select id, name from accessories
where type like '1;%' or type like '%;1' or type like '%;1;%';
结果:
+----+---------+
| id | name |
+----+---------+
| 1 | seats |
| 3 | tires |
+----+---------+
关于mysql - 如何在以分号分隔的字段中查找具有给定值的记录-MySQL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49417604/