【问题标题】:MYSQL - Select most recent of column type entryMYSQL - 选择最近的列类型条目
【发布时间】:2012-06-23 06:52:08
【问题描述】:

我有一个包含列的表格:
s_Id (主要 int), sup_type (int), sup_date (datetime), sup_req (int)

sup_type 中的值目前范围为 0-5。

如何提取每个 sup_type (0-5) 的最新条目(按 sup_date)。

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*  s_Id   *  sup_type   *        sup_date         *  sup_req  *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*    1    *      0      *   2012-06-15 10:13:21   *     4     *
*    2    *      0      *   2012-06-15 11:22:01   *     4     *
*    3    *      1      *   2012-06-15 13:23:32   *     4     *
*    4    *      2      *   2012-06-16 08:04:29   *     4     *
*    5    *      1      *   2012-06-16 16:23:24   *     4     *
*    6    *      1      *   2012-06-17 13:14:05   *     4     *
*    7    *      3      *   2012-06-18 13:37:55   *     4     *
*    8    *      4      *   2012-06-19 13:21:52   *     4     *
*    9    *      4      *   2012-06-20 16:15:19   *     4     *
*   10    *      5      *   2012-06-20 16:17:37   *     4     *
*   11    *      0      *   2012-06-20 16:21:53   *     4     *
*   12    *      1      *   2012-06-20 16:28:13   *     4     *
*   13    *      3      *   2012-06-21 12:23:29   *     4     *
*   14    *      3      *   2012-06-22 07:26:41   *     4     *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

我要提取

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*  s_Id   *  sup_type   *        sup_date         *  sup_req  *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*    4    *      2      *   2012-06-16 08:04:29   *     4     *
*    9    *      4      *   2012-06-20 16:15:19   *     4     *
*   10    *      5      *   2012-06-20 16:17:37   *     4     *
*   11    *      0      *   2012-06-20 16:21:53   *     4     *
*   12    *      1      *   2012-06-20 16:28:13   *     4     *
*   14    *      3      *   2012-06-22 07:26:41   *     4     *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

谢谢。

【问题讨论】:

    标签: mysql select limit


    【解决方案1】:
    SELECT
        b.*
    FROM
        (
            SELECT sup_type, MAX(sup_date) AS maxsupdate
            FROM tbl
            GROUP BY sup_type
        ) a
    INNER JOIN
        tbl b ON 
            a.sup_type = b.sup_type AND 
            a.maxsupdate = b.sup_date
    ORDER BY
        b.s_Id
    

    【讨论】:

    • 是的。并在(sup_type, sup_date) 上建立索引以提高效率。
    猜你喜欢
    • 2017-03-19
    • 1970-01-01
    • 2020-10-21
    • 2012-01-07
    • 2013-08-10
    • 1970-01-01
    • 2012-03-17
    • 2016-05-08
    • 1970-01-01
    相关资源
    最近更新 更多