选择时间戳和相应值中每天的最后一个条目

时间:2014-06-13 08:36:51

标签: mysql sql join

我有一张名为' admin_tmp'具有以下结构

Field                 | Type             | Null | Key | Default | Extra
----------------------+------------------+------+-----+---------+---------------
id                    | int(10) unsigned | NO   | PRI | NULL    | auto_increment
time_stamp            | varchar(30)      | NO   |     | NULL    |
curr_property         | int(5) unsigned  | NO   |     | NULL    |
curr_property_cost    | int(5) unsigned  | NO   |     | NULL    |
day_property          | int(S) unsigned  | NO   |     | NULL    |
day_property_cost     | int(5) unsigned  | NO   |     | NULL    |
curr_solar_generating | int(5) unsigned  | NO   |     | NULL    |
curr_solar_export     | int(5) unsigned  | NO   |     | NULL    |
day_solar_generated   | int(5) unsigned  | NO   |     | NULL    |
day_solar_export      | int(5) unsigned  | NO   |     | NULL    |
curr_chanl            | int(5) unsigned  | NO   |     | NULL    |
curr_chan2            | int(5) unsigned  | NO   |     | NULL    |
curr_chan3            | int(5) unsigned  | NO   |     | NULL    |
day chan1             | int(5) unsigned  | NO   |     | NULL    |
day_chan2             | int(5) unsigned  | NO   |     | NULL    |
day_chan3             | int(5) unsigned  | NO   |     | NULL    |

现在我想在每天的两个时间值7:0:0和7:59:59(只有日期而不是时间戳)之间选择最后一个条目及其相应的值

我只能使用以下查询

获取以下内容

查询

SELECT id, time_stamp , curr_property, day_property, mytime , mydate 
FROM (
SELECT 
*
,DATE(time_stamp) AS mydate
,TIME(time_stamp) AS mytime
FROM admin_tmp
) AS Result
WHERE mytime >= '07:00:00' AND mytime <= '07:59:59'
Order By mytime LIMIT 15;

结果

   id | time_stamp          | curr_property | day_property | mytime   | mydate    
------+---------------------+---------------+--------------+----------+-----------
 1225 | 2014-06-01 07:00:04 |          1641 |        11466 | 07:00:04 | 2014-06-01
13802 | 2014-06-03 07:00:05 |          1850 |        15452 | 07:00:05 | 2014-06-03
 7418 | 2014-06-02 07:00:05 |          1577 |        13053 | 07:00:05 | 2014-06-02
 1226 | 2014-06-01 07:00:16 |          1593 |        11471 | 07:00:16 | 2014-06-01
13803 | 2014-06-03 07:00:17 |          1577 |        15457 | 07:00:17 | 2014-06-03
 7419 | 2014-06-02 07:00:17 |          1528 |        13058 | 07:00:17 | 2014-06-02
 1227 | 2014-06-01 07:00:28 |          1577 |        11476 | 07:00:28 | 2014-06-01
 7420 | 2014-06-02 07:00:29 |          1545 |        13063 | 07:00:29 | 2014-06-02
13804 | 2014-06-03 07:00:29 |          1850 |        15464 | 07:00:29 | 2014-06-03
 1228 | 2014-06-01 07:00:40 |           981 |        11480 | 07:00:40 | 2014-06-01
13805 | 2014-06-03 07:00:41 |          1561 |        15469 | 07:00:41 | 2014-06-03
 7421 | 2014-06-02 07:00:41 |          1577 |        13069 | 07:00:41 | 2014-06-02
 1229 | 2014-06-01 07:00:52 |          1206 |        11484 | 07:00:52 | 2014-06-01
 7422 | 2014-06-02 07:00:53 |          1399 |        13073 | 07:00:53 | 2014-06-02
13806 | 2014-06-03 07:00:53 |          1545 |        15474 | 07:00:53 | 2014-06-02

我开始知道我必须使用join才能实现这一目标,但我再也无法使用join而没有任何错误。

2 个答案:

答案 0 :(得分:2)

您可以选择相同的数据两次,一次获取记录,一次获得每个日期的最长时间。然后加入两者,这样你就可以过滤以获得每个日期最长时间的记录。

SELECT 
  rec.id, 
  rec.time_stamp, 
  rec.curr_property, 
  rec.day_property, 
  rec.mytime, 
  rec.mydate
FROM 
(
  SELECT 
    admin_tmp.*,
    DATE(time_stamp) AS mydate,
    TIME(time_stamp) AS mytime
  FROM admin_tmp
  WHERE TIME(time_stamp) >= '07:00:00' AND TIME(time_stamp) <= '07:59:59'
) as rec
JOIN
(
  SELECT 
    DATE(time_stamp) as mydate,
    MAX(TIME(time_stamp)) as mytime
  FROM admin_tmp
  WHERE TIME(time_stamp) >= '07:00:00' AND TIME(time_stamp) <= '07:59:59'
  GROUP BY DATE(time_stamp)
) as max_times ON max_times.mydate = rec.mydate and max_times.mytime = rec.mytime
ORDER BY rec.mytime LIMIT 15;

答案 1 :(得分:1)

试试这个会给你每天的最新记录。

http://sqlfiddle.com/#!2/49aff3/3

select i1.* from InverterReadings i1
LEFT JOIN InverterReadings i2 
            on i2.myDate=i1.myDate and i1.myTime<i2.myTime
where i2.id IS NULL;