Mysql选择最近的每个艺术家的日期desc

时间:2017-06-18 09:32:03

标签: mysql sql mysqli

我有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           |
+---------+--------------------+

现在,我需要很好的查询这个转发。我想从(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) 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 | 
| 108     | metallica    |  2009-01-12 00:00:00 | 
| 101     | metallica    |  2002-01-12 00:00:00 | 
+---------+--------------+----------------------+

修改

此查询正在运行但排序日期desc无效:

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`)

2 个答案:

答案 0 :(得分:1)

如果我发现了问题,查询将如下所示:

interface User {
  age?: number,
  name: string,
  birthdate: string    // should have format 'YYYY-MM-DD'
}

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

答案 1 :(得分:1)

在MySQL中,您可以使用变量执行此操作:

[3,4,5] (Array)

在内部true (TrueClass)中,您可以指定您喜欢的任何艺术家或其他条件。