mysql - 如何获取所选组的最新N条记录

标签 mysql sql date group-by sql-order-by

我想在 MySql version 5.1.9 上运行查询,该查询仅返回所选部门的前两个(按 JoiningDate 排序)。

例如,我的数据如下:

+-------+------------------------------------------+----------+------------+
| empid | title                                    | Dept     | JoiningDate|
+-------+------------------------------------------+----------+------------+
|   1   | Research and Development                 | 1        | 2015-08-06 |
|   2   | Consultant                               | 2        | 2015-08-06 |
|   3   | Medical Consultant                       | 3        | 2015-08-06 |
|   4   | Officer                                  | 4        | 2015-08-06 |
|   5   | English Translator                       | 5        | 2015-08-06 |
|   6   | Teacher                                  | 1        | 2015-08-01 |
|   7   | Physical Education                       | 2        | 2015-08-01 |
|   8   | Accountant                               | 3        | 2015-08-01 |
|   9   | Science Teacher                          | 4        | 2015-08-01 |
|  10   | Home Science                             | 5        | 2015-08-01 |
|  11   | Research Assistant                       | 1        | 2015-08-05 |
|  12   | Consultant                               | 2        | 2015-08-05 |
|  13   | Consultant HR                            | 3        | 2015-08-05 |
|  14   | Technical Lead                           | 4        | 2015-08-05 |
|  15   | Hindi Translator                         | 5        | 2015-08-05 |
|  16   | Urdu Teacher                             | 1        | 2015-08-02 |
|  17   | Physical Education                       | 2        | 2015-08-02 |
|  18   | Accountant                               | 3        | 2015-08-02 |
|  19   | Science                                  | 4        | 2015-08-02 |
|  20   | Home Science                             | 5        | 2015-08-02 |
+-------+------------------------------------------+----------+------------+

我希望查询输出 Dept (1,2,3) 的最新连接的两个 empid,即:

+-------+------------------------------------------+----------+------------+
| empid | title                                    | Dept     | JoiningDate|
+-------+------------------------------------------+----------+------------+
|   1   | Research and Development                 | 1        | 2015-08-06 |
|  11   | Research Assistant                       | 1        | 2015-08-05 |
|   2   | Consultant                               | 2        | 2015-08-06 |
|  12   | Consultant                               | 2        | 2015-08-05 |
|   3   | Medical Consultant                       | 3        | 2015-08-06 |
|  13   | Consultant HR                            | 3        | 2015-08-05 |
+-------+------------------------------------------+----------+------------+

最佳答案

在mysql中你可以使用用户定义的变量来达到你想要的结果

SELECT 
t.empid,
t.title,
t.Dept,
t.JoiningDate
FROM
(
    SELECT 
    *,
    @r:= CASE WHEN @g = b.Dept THEN @r + 1 ELSE 1 END rounum,
    @g:= b.Dept
    FROM (
        SELECT * 
        FROM table1 
        CROSS JOIN (SELECT @r:= NULL,@g:=NULL) a
        WHERE Dept IN(1,2,3)
        ORDER BY Dept,JoiningDate DESC
    ) b
) t
WHERE t.rounum <=2

DEMO

关于mysql - 如何获取所选组的最新N条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32738654/

相关文章:

mysql - 从 select 语句插入日期时出现问题错误代码 : 1292 MySQL

Mysql 日期加密

ios - 在 iOS 中将日期转换为时间戳

MySQL:排除子查询中收集的同一个表中的项目

mysql - MySQL中如何将一个表拆分为两个?

mysql - 如何保证auto_increment数字没有间隙?

php - mysql_real_escape_string 删除整个字符串

javascript - 使用 Moment 获取 Date 对象的月份

mysql - 语法错误或访问冲突 : 1055 Expression #8 of SELECT list is not in GROUP BY clause and contains nonaggregated column

mysql - sql聊天组相同的聊天