我正在尝试获取每一天的最新时间(end_time),然后使用正确的trans_num(id#)和log_message发布....有点可行,但有一些问题..
这是一张数据图片:
这是我的代码:
SELECT max(start_time) as start_time, max(end_time) as end_time, uid, trans_num, log_message FROM table1 WHERE uid='admin' AND start_time BETWEEN '2013-09-29 00:00:00' AND '2013-10-05 23:59:59' GROUP BY DATE(end_time)
结果是:
正在选择正确的最新时间但是2013-10-02的trans_num是错误的...它假设为757而且log_message也是错误的...它应该是“jj”
任何人都可以帮助我吗?
答案 0 :(得分:1)
因此,您需要将所选的end_time
与相应的trans_num
和log_message
相匹配。你可以通过初始表JOIN
结果来做到这一点:
SELECT
a.st,
a.et,
t1.trans_num,
t1.log_message
FROM
(SELECT
max(start_time) as st,
max(end_time) as et
FROM table1
WHERE uid='admin' AND start_time BETWEEN '2013-09-29 00:00:00' AND '2013-10-05 23:59:59'
GROUP BY DATE(end_time)) a
JOIN
table1 t1 ON a.et=t1.end_time