我有2张桌子。类别表中的“ media_id”引用媒体表中的“ id”。媒体表中的每个项目可以具有来自类别表的零个或多个类别。
媒体表
| id | options | order_id | player_id |
|-------------------------------------|
| 1 | ... | 0 | 5 |
|-------------------------------------|
| 2 | ... | 1 | 11 |
|-------------------------------------|
| 3 | ... | 2 | 7 |
|_____________________________________|
类别表
__________________________
| id | category | media_id |
|--------------------------|
| 1 | fashion | 1 |
|--------------------------|
| 2 | sport | 2 |
|--------------------------|
| 3 | sport | 3 |
|--------------------------|
| 4 | cats | 4 |
|--------------------------|
| 5 | dogs | 4 |
|--------------------------|
| 6 | sport | 5 |
|__________________________|
我是否要在两个表中从单个查询中选择player_id = 5 ORDER BY order_id的所有数据?
响应应为array(id,options,order_id,category)
我可以运行一个查询以从player_id = 5的媒体表中获取所有项目。然后,我将循环所有结果,并为每个结果从类别表中选择所有类别,其中媒体表中的media_id = id。但这将是多个查询。
你有尝试过吗?
SELECT *
FROM media
INNER JOIN category
ON media.id = category.media_id
WHERE media.player_id = 5
ORDER BY media.order_id
不确定这是否就是您要找的东西...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句