SQL - 选择电影最多的播放列表

时间:2013-01-12 15:06:18

标签: sql database hsqldb

我正在努力查询。

我有两张桌子:

CREATE TABLE Playlist(
    id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
);



CREATE TABLE PlaylistMovies(
    mid INTEGER FOREIGN KEY REFERENCES Movie(movieid) ON DELETE CASCADE,
    pid INTEGER FOREIGN KEY REFERENCES Playlist(id) ON DELETE CASCADE,   
    PRIMARY KEY (mid, pid)
);

我想找到电影数量最多的播放列表(计数器中段)。

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

请试试这个:

查询:

select top 1 pl.pid, p.name,
         count(pl.mid) as count_id
  from playlistmovies pl
  left join playlist p
  on p.id = pl.pid
  group by pl.pid, p.name
  order by count_id desc
;

结果:

| PID | NAME | COUNT_ID |
-------------------------
|   1 |   p1 |        5 |