我进行了很多搜索来找到答案,但我找不到。这是我的问题
我有一个名为 main_table 的表,如下所示:
╔════╦════════════════╦
║ id ║ seat_id ║
╠════╬════════════════╬
║ 1 ║ 274115, ║
║ 2 ║ 274116,274117,║
║ 3 ║ 274113,274114, ║
╚════╩════════════════╩
这些 Seat_id 是另一个名为 sub_table 的表的主键
╔═════════╦════════════════╦
║ seat_id ║ seat ║
╠═════════╬════════════════╬
║ 274115 ║ 186 ║
║ 274116 ║ 187 ║
║ 274117 ║ 188 ║
║ 274118 ║ 159 ║
╚═════════╩════════════════╩
我想要与main_table的seat_id相关的所有座位
╔════════════╗
║ seat ║
╠════════════╣
║ 186 ║
║ 187 ║
║ 188 ║
╚════════════╝
到目前为止我尝试过的是子查询
select seat from sub_table where seat_id in(select seat_id from main_table)
这对我没有帮助
最佳答案
您可以使用FIND_IN_SET
:
SELECT seat
FROM sub_table AS t1
WHERE EXISTS (SELECT 1
FROM main_table AS t2
WHERE FIND_IN_SET(t1.seat_id, t2.seat_id) <> 0)
但是,我建议规范化表main_table
,因为像您一样将逗号分隔的值存储在单个字段中始终是一个坏主意。
关于mysql - 在 mysql 中进行比较的逗号分隔字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36906391/