gpt4 book ai didi

Mysql 为每位艺术家选择最近的日期和日期

转载 作者:行者123 更新时间:2023-11-29 06:58:32 25 4
gpt4 key购买 nike

我有 3 张 table :

  • PK:主键
  • FK:外键

mp3表:

+---------+--------------+----------------------+
| id (PK) | tarck_title | date |
+---------+--------------+----------------------+
| 100 | shakira | 2001-01-12 00:00:00 |
| 101 | metallica | 2002-01-12 00:00:00 |
| 102 | james blunt | 2003-01-12 00:00:00 |
| 103 | shakira | 2004-01-12 00:00:00 |
| 104 | anathema | 2005-01-12 00:00:00 |
| 105 | nelson | 2006-01-12 00:00:00 |
| 106 | shakira | 2007-01-12 00:00:00 |
| 107 | bb king | 2008-01-12 00:00:00 |
| 108 | metallica | 2009-01-12 00:00:00 |
| 109 | nelson | 2010-01-12 00:00:00 |
| 110 | shakira | 2011-01-12 00:00:00 |
| 111 | bb king | 2012-01-12 00:00:00 |
+---------+--------------+----------------------+

艺术家表:

+---------+----------------+
| id (PK) | artist_name |
+---------+----------------+
| 14 | shakira |
| 221 | metallica |
| 320 | james blunt |
| 328 | shakira |
| 1004 | anathema |
| 1140 | nelson |
| 1401 | bb king |
+---------+----------------+

标签表:PK(mp3_id,artist_id)

+-------------+----------------+
| mp3_id (FK) | artist_id (FK) |
+-------------+----------------+
| 100 | 14 |
| 101 | 221 |
| 102 | 320 |
| 103 | 14 |
| 104 | 1004 |
| 105 | 1140 |
| 106 | 14 |
| 107 | 1401 |
| 108 | 221 |
| 109 | 1140 |
| 110 | 14 |
| 111 | 1401 |
+---------+--------------------+

现在,我需要对此结果进行良好的查询。我想从 (shakira & bb king) order by date 轨道中选择 3 首最新轨道。像这样:

+---------+--------------+----------------------+
| id (PK) | tarck_title | date |
+---------+--------------+----------------------+
| 110 | shakira | 2011-01-12 00:00:00 |
| 106 | shakira | 2007-01-12 00:00:00 |
| 103 | shakira | 2004-01-12 00:00:00 |
| 111 | bb king | 2012-01-12 00:00:00 |
| 107 | bb king | 2008-01-12 00:00:00 |
+---------+--------------+----------------------+

或从(shakira & bb king &Metallica)按日期排序轨道中选择 3 首最新轨道。像这样:

+---------+--------------+----------------------+
| id (PK) | tarck_title | date |
+---------+--------------+----------------------+
| 110 | shakira | 2011-01-12 00:00:00 |
| 106 | shakira | 2007-01-12 00:00:00 |
| 103 | shakira | 2004-01-12 00:00:00 |
| 111 | bb king | 2012-01-12 00:00:00 |
| 107 | bb king | 2008-01-12 00:00:00 |
| 108 | metallica | 2009-01-12 00:00:00 |
| 101 | metallica | 2002-01-12 00:00:00 |
+---------+--------------+----------------------+

编辑:

此查询有效,但排序日期降序无效:

SELECT `id`, `tarck_title`, `date`
FROM `mp3s`
WHERE `id` IN (
SELECT x.`mp3_id`
FROM `tags` x
INNER JOIN `tags` y ON y.`artist_id` = x.`artist_id` AND y.`mp3_id` <= x.`mp3_id`
INNER JOIN `mp3s` z ON z.`id` = x.`mp3_id`
WHERE x.`artist_id` IN (SELECT `artist_id` FROM `tags` WHERE `mp3_id` = 103)
GROUP BY x.`mp3_id` HAVING COUNT(*) <= 3
ORDER BY z.`date` DESC, x.`artist_id` DESC, x.`mp3_id`)

最佳答案

如果我找到了这个问题,查询将如下所示:

  (SELECT m.* FROM (tags as t JOIN artists as a on t.artist_id = a.id) JOIN mp3s as m on m.id = t.mp3_id
WHERE a.artist_name = 'Shakira'
ORDER BY m.date DESC
LIMIT 3)
UNION
(SELECT m.* FROM (tags as t JOIN artists as a on t.artist_id = a.id) JOIN mp3s as m on m.id = t.mp3_id
WHERE a.artist_name = 'bb King'
ORDER BY m.date DESC
LIMIT 3);

此外,如果您想要查询的压缩格式,您可以在 this post 中找到它。 。

关于Mysql 为每位艺术家选择最近的日期和日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44613617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com