假设某个网站根据发布的媒体格式对热门故事进行分类。其数据库由以下表格组成:
应该使用哪种查询或查询来检索以电影形式发布的所有故事片?
我想过做这样的事情,但怀疑有更好的方法:
SELECT `story_id`
FROM `links` INNER JOIN `media`
ON `links`.`medium_id` = `media`.`medium_id`
WHERE `media`.`medium_name` = :medium_name"
for every retrieved story_id
{
SELECT `story_summary` FROM `stories` WHERE `story_id` = :story_id
}
非常感谢任何帮助!
答案 0 :(得分:1)
尝试使用此查询(我添加了第三行和最后一行):
SELECT `story_id` FROM `links`
INNER JOIN `media` ON `links`.`medium_id` = `media`.`medium_id`
inner join stories on stories.story_ID=links.story_id
WHERE `media`.`medium_name` = :medium_name
and `story_summary`.`story_id` in (:all_the_story_id)
: all_the_story_id 那里放了所有的story_id,看看mysql in
如果您进行额外加入,您将获得所需的所有信息,并且您不需要对每个结果进行查询
答案 1 :(得分:1)
SELECT
s.story_title
FROM MEDIA as m
LEFT JOIN LINKS as l ON l.medium_id = m.medium_id
LEFT JOIN STORIES as s ON s.story_id = l.story_id
WHERE m.medium_name = 'movie'
答案 2 :(得分:0)
select s.story_title
from stories s, links l, media m
where m.medium_name = 'movie' and
s.story_id = l.story_id and
l.medium_id = m.medium_id
这与raheel发布的查询基本相同,只是语法不同。 故事和媒体表没有直接加入是有原因的吗?您的查询和数据库可以简化。