简体   繁体   中英

Select 2nd From Last Entry In MySQL

Problem : Need 2nd from last entry from each distinct ticket within a specified time period

Example Data :

ticketTB
ticket createdate          status
111    2015-08-13 04:05:12 good
111    2015-08-13 04:04:12 bad
111    2015-08-13 04:03:12 good
115    2015-08-13 03:05:12 good
115    2015-08-13 03:04:12 bad
115    2015-08-13 03:03:12 good

Query :

SELECT ticket, status, createdate FROM ticketTB GROUP BY ticket ORDER BY createdate DESC LIMIT 1,1;

This query pulls:

ticket createdate          status
111    2015-08-13 04:04:12 bad

However, add in many other tickets it will only pull the 2nd from last of all of those I need it to evaluate every distinct ticket.

I want my query to return:

ticket createdate          status
111    2015-08-13 04:04:12 bad
115    2015-08-13 03:04:12 bad
DROP TABLE IF EXISTS ticketTB;

CREATE TABLE ticketTB
(ticket INT NOT NULL
,createdate DATETIME NOT NULL
,status VARCHAR(12)
,PRIMARY KEY(ticket,createdate)
);

INSERT INTO ticketTB VALUES
(111    ,'2015-08-13 04:05:12','good'),
(111    ,'2015-08-13 04:04:12','bad'),
(111    ,'2015-08-13 04:03:12','good'),
(111    ,'2015-08-13 04:02:12','good'),
(115    ,'2015-08-13 03:05:12','good'),
(115    ,'2015-08-13 03:04:12','bad'),
(115    ,'2015-08-13 03:03:12','good'),
(115    ,'2015-08-13 03:02:12','good');

SELECT * FROM ticketTB;
+--------+---------------------+--------+
| ticket | createdate          | status |
+--------+---------------------+--------+
|    111 | 2015-08-13 04:02:12 | good   |
|    111 | 2015-08-13 04:03:12 | good   |
|    111 | 2015-08-13 04:04:12 | bad    |
|    111 | 2015-08-13 04:05:12 | good   |
|    115 | 2015-08-13 03:02:12 | good   |
|    115 | 2015-08-13 03:03:12 | good   |
|    115 | 2015-08-13 03:04:12 | bad    |
|    115 | 2015-08-13 03:05:12 | good   |
+--------+---------------------+--------+

SELECT x.* 
  FROM ticketTB x 
  JOIN ticketTB y 
    ON y.ticket = x.ticket 
   AND y.createdate >= x.createdate 
 GROUP 
    BY ticket
     , createdate 
HAVING COUNT(*) = 2;
+--------+---------------------+--------+
| ticket | createdate          | status |
+--------+---------------------+--------+
|    111 | 2015-08-13 04:04:12 | bad    |
|    115 | 2015-08-13 03:04:12 | bad    |
+--------+---------------------+--------+

Something like the following method will generally be faster, especially so on larger data sets...

SELECT ticket
     , createdate
     , status 
  FROM
     ( SELECT *
            , CASE WHEN @prev = ticket THEN @i:=@i+1 ELSE @i:=1 END rank
            , @prev := ticket prev 
         FROM ticketTB
            , (SELECT @prev:='',@i:=1) vars 
        ORDER 
           BY ticket
            , createdate DESC
     ) x 
 WHERE rank = 2;
+--------+---------------------+--------+
| ticket | createdate          | status |
+--------+---------------------+--------+
|    111 | 2015-08-13 04:04:12 | bad    |
|    115 | 2015-08-13 03:04:12 | bad    |
+--------+---------------------+--------+

尝试这个:

SELECT * FROM my_table ORDER BY rating DESC LIMIT 2,1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM