我的表结构是:
SELECT * FROM `MYTABLE` WHERE `USERID` = 148 ORDER BY `TABLEDATE` DESC;
ID USERID STATE_ID TABLEDATE
96 148 13 2013-01-08 06:11:34
554 148 3 2013-01-03 04:46:08
25 148 2 2013-01-02 04:46:07
我希望用户在“MYTABLE”表中输入最高秒数(=>按STATE_ID DESC限制1,1排序),其日期最后一个条目的日期为“2013-01-08”。
我尝试形成查询但失败了。请帮忙吗?
SELECT F1.USERID,
MAX(F2.TABLEDATE),
F2.STATE_ID
FROM MYTABLE F1
JOIN MYTABLE F2 ON F1.USERID = F2.USERID WHERE
F1.STATE_ID = 13 AND
F2.TABLEDATE < F1.TABLEDATE AND
F1.TABLEDATE <= '2013-01-08 23:59:59' AND
F1.TABLEDATE >= '2013-01-08 00:00:00'
GROUP BY F1.USERID
输出
148 2013-01-03 04:46:08 2
预期输出
148 2013-01-03 04:46:08 3
答案 0 :(得分:0)
如果您想要具有第二高值State_ID
的行,您只需执行此操作:
SELECT *
FROM
(
SELECT *
FROM yourTable
LIMIT 2
) AS sub
ORDER BY State_ID
LIMIT 1;