mysql - 从 MySQL 中获取对应于某种模式的数据

标签 mysql sql database

我有两个 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/

相关文章:

mysql - 当行不存在时显示计数值 0 MYSQL

php - 删除PHP MySQL查询错误

php - 无法连接mysql

sql - 如果利润小于项目成本的 55%,如何编写这个 select 语句?

mysql - 在 MySQL/Oracle 数据库中切换 bool 值

php - 如何将输入类型文件(即图像)目录作为 session 传递

c# - 更新现有行 mysql linq

c# - 使用不同的数据库,具有不同的DBcontext,并根据不同的URL请求连接到它

sql - 如何在同一个表中组合多个查询

sql - Firebird SQL 多列索引