MySQL:在分组数据上使用 "inner join" 和 "count" 创建视图
MySQL: Create view with "inner join" and "count" on grouped data
我的数据库包含一个包含许多不同数据列的 table。以下简单表示显示了我当前感兴趣的列:
Table 播放列表:
id metadata lastplayed
===============================================
1 All Night 1571324631
2 Perfect Replacement 1571324767
3 One More Day 1571324952
4 Stay Awake 1571325184
5 Perfect Replacement 1571325386
6 All Night 1571325771
7 Close Enemies 1571326422
我已经有了一个对元数据进行分组的视图,因此我可以看到歌曲的所有单次出现以及它们上次播放的时间(纪元秒)。
查看 'Music'(期望的结果):
id metadata lastplayed count (*)
==============================================================
3 One More Day 1571324952 1
4 Stay Awake 1571325184 1
5 Perfect Replacement 1571325386 2
6 All Night 1571325771 2
7 Close Enemies 1571326422 1
“计数”列在视图中尚不存在,我想通过创建视图的现有 SQL 脚本将其包括在内:
CREATE VIEW `Music` AS
SELECT
t1.`id`,
t1.`metadata`,
t1.`lastplayed`
FROM Playlist t1
INNER JOIN
(
SELECT `metadata`, MAX(`lastplayed`) AS `timestamp`
FROM Playlist
GROUP BY `metadata`
) t2
ON t1.`metadata` = t2.`metadata` AND t1.`lastplayed` = t2.`timestamp`
ORDER BY t1.`Id` ASC
所以现在我 运行 陷入了将我的 COUNT(metadata) AS count
行放在何处以及如何放置的问题,以获得所需的结果。当我将它添加到顶部 SELECT 行时,table 减少为包含一首歌曲和所有数据行数的单个数据行。
放在里面select
CREATE VIEW `Music` AS
SELECT
t1.`id`,
t1.`metadata`,
t2.`lastplayed`,
t2.count
FROM Playlist t1
INNER JOIN
(
SELECT `metadata`, MAX(`lastplayed`) AS `timestamp`, COUNT(*) AS count
FROM Playlist
GROUP BY `metadata`
) t2
ON t1.`metadata` = t2.`metadata` AND t1.`lastplayed` = t2.`timestamp`
ORDER BY t1.`Id` ASC
您只需要通过按 metadata
列
分组进行简单聚合
CREATE OR REPLACE VIEW `Music` AS
SELECT MAX(id) AS id, `metadata`, MAX(`lastplayed`) AS lastplayed, COUNT(*) AS count
FROM Playlist
GROUP BY `metadata`
我的数据库包含一个包含许多不同数据列的 table。以下简单表示显示了我当前感兴趣的列:
Table 播放列表:
id metadata lastplayed
===============================================
1 All Night 1571324631
2 Perfect Replacement 1571324767
3 One More Day 1571324952
4 Stay Awake 1571325184
5 Perfect Replacement 1571325386
6 All Night 1571325771
7 Close Enemies 1571326422
我已经有了一个对元数据进行分组的视图,因此我可以看到歌曲的所有单次出现以及它们上次播放的时间(纪元秒)。
查看 'Music'(期望的结果):
id metadata lastplayed count (*)
==============================================================
3 One More Day 1571324952 1
4 Stay Awake 1571325184 1
5 Perfect Replacement 1571325386 2
6 All Night 1571325771 2
7 Close Enemies 1571326422 1
“计数”列在视图中尚不存在,我想通过创建视图的现有 SQL 脚本将其包括在内:
CREATE VIEW `Music` AS
SELECT
t1.`id`,
t1.`metadata`,
t1.`lastplayed`
FROM Playlist t1
INNER JOIN
(
SELECT `metadata`, MAX(`lastplayed`) AS `timestamp`
FROM Playlist
GROUP BY `metadata`
) t2
ON t1.`metadata` = t2.`metadata` AND t1.`lastplayed` = t2.`timestamp`
ORDER BY t1.`Id` ASC
所以现在我 运行 陷入了将我的 COUNT(metadata) AS count
行放在何处以及如何放置的问题,以获得所需的结果。当我将它添加到顶部 SELECT 行时,table 减少为包含一首歌曲和所有数据行数的单个数据行。
放在里面select
CREATE VIEW `Music` AS
SELECT
t1.`id`,
t1.`metadata`,
t2.`lastplayed`,
t2.count
FROM Playlist t1
INNER JOIN
(
SELECT `metadata`, MAX(`lastplayed`) AS `timestamp`, COUNT(*) AS count
FROM Playlist
GROUP BY `metadata`
) t2
ON t1.`metadata` = t2.`metadata` AND t1.`lastplayed` = t2.`timestamp`
ORDER BY t1.`Id` ASC
您只需要通过按 metadata
列
CREATE OR REPLACE VIEW `Music` AS
SELECT MAX(id) AS id, `metadata`, MAX(`lastplayed`) AS lastplayed, COUNT(*) AS count
FROM Playlist
GROUP BY `metadata`