0

假设一个网站根据发布的媒体格式对热门故事进行分类。其数据库由以下表格组成:

故事表

链接表

在此处输入图像描述

应该使用什么样的查询来检索以电影形式发布的所有故事标题?

我想过做这样的事情,但怀疑有更好的方法:

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
}

任何帮助将非常感激!

4

3 回答 3

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里面

如果您进行额外加入,您将获得所需的所有信息,并且您无需为每个结果进行查询

于 2013-01-18T19:15:04.773 回答
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'
于 2013-01-18T19:22:25.740 回答
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 发布的查询相同,只是语法不同。故事和中表没有直接连接是否有原因?您的查询和数据库可以简化。

于 2013-01-18T19:49:09.927 回答