我有两个 mysql 表:
表A
id | name | favorite
1 | John | 4;1;2;
等...
表B
id | label | product
1 | game | Xbox
2 | game | Playstation
3 | game | Wii
预期结果
表 C
2 | game | Playstation
3 | game | Wii
我需要从表 A 中获取与最喜欢的列(数据由半列分隔)和表 B 中的 ID 行列相对应的数据
mysql中有没有foreach循环可以做这种类型的查询?
最佳答案
您的表格设计未规范化。添加新表
user_favorites table
--------------------
user_id
product_id
示例数据:
user_id | product_id
1 | 4
1 | 1
1 | 2
然后你可以得到你喜欢的结果
select b.*
from tableb b
join user_favorites f on b.id = f.product_id
join tablea a on f.user_id = a.id
where a.name = 'john'
关于mysql - 从 MySQL 中获取对应于某种模式的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27409176/