【问题标题】:Join three rows with one row from another table将三行与另一表中的一行连接起来
【发布时间】:2017-08-07 16:07:31
【问题描述】:

我正在做一个家庭项目。在我的数据库中,我有两个名为电影和类别的表。在电影表中有名为 category、category_two、category_three 的三行。并且在类别表中只有一个行名category_name。 我想将电影表中的所有三行与类别表中名为 category_table 的行连接起来。 但我不知道如何进行该查询。 请帮我。

【问题讨论】:

标签: php mysql mysqli


【解决方案1】:

你应该这样做:

movies table
------------
id
name


categories table
----------------
id
name


movie_categories table
----------------------
movie_id
category_id

像这样,您可以为一部电影设置任意数量的类别。然后要获取特定电影及其类别,您可以执行以下操作:

select m.*, c.name as category_name
from movies m
left join movie_categories mc on m.id = mc.movie_id
left join categories c on c.id = mc.category_id
where m.name = 'star wars'

【讨论】:

    猜你喜欢
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 2012-02-17
    相关资源
    最近更新 更多