>我有 3 个表:
- PK:主键
- FK:外键
MP3S 表:
+---------+--------------+----------------------+
| 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 |
+---------+--------------------+
现在,我需要对这个资源进行良好的查询。 我想从(夏奇拉和BB King(中选择3首最新曲目 order by date
曲目。 像这样:
+---------+--------------+----------------------+
| 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首最新曲目order by date
曲目。
+---------+--------------+----------------------+
| 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);
另外,如果您想要查询的压缩格式,可以在这篇文章中找到它。
在MySQL中,你可以用变量来做到这一点:
SELECT m.*
FROM (SELECT m.*,
(@rn := if(@a = a.id, @rn + 1,
if(@a := a.id, 1, 1)
)
) as rn
FROM tags t JOIN
artists a
ON t.artist_id = a.id JOIN
mp3s m
ON m.id = t.mp3_id CROSS JOIN
(SELECT @a := -1, @rn := 0) params
WHERE . . .
ORDER BY a.id, m.date DESC
) m
WHERE rn <= 3;
在 内部WHERE
中,您可以指定您喜欢的任何艺术家或其他条件。