0

我有 2 个 SQL 表:"episode"并且"topic",每个主题和剧集都有一个视频(一集包含多个主题,但对我的问题并不重要)。他们有自己的领域,但也有"title", "date", "video_url"共同点"count"(计数是观看视频的次数)。

我想根据计数字段仅使用一个查询进行前 5 位选择。

所以我需要合并查询:

select title, date, video_url, count from episode order by count desc 

和“来自主题”的相同查询。

4

1 回答 1

2
 select top 5 *
 from
 (
      select title, date, video_url, count from episode
      union all
      select title, date, video_url, count from topic
 ) as A
 order by count desc

您可以添加类型名称以找出它是剧集还是主题。您也可以将top5 添加到子查询中,以便服务器知道他不需要获取所有记录

 select top 5 *
 from
 (
      select top 5 title, date, video_url, count, 'episode' as type_name from episode order by count desc
      union all
      select top 5 title, date, video_url, count, 'topic' as type_name from topic order by count desc
 ) as A
 order by count desc
于 2012-10-25T12:40:37.603 回答