获取每个项目的最近日期值组

时间:2014-03-31 13:27:30

标签: php mysql datetime

表t1具有以下结构

|  id  |      from             |  item   |    p    |
    1    2014-03-26 08:00:00       500        9
    2    2014-03-28 14:30:00       500        7
    3    2014-03-29 14:30:00       200        48
    4    2014-04-01 19:00:00       200        51
    5    2014-03-30 23:30:00       500        6

如何为每个项目选择唯一一个日期最接近现在过去的记录?

SELECT t1.`from`, t1.item, t1.item 
  FROM t1
WHERE t1.`from` <= NOW()
  ORDER BY `from` DESC
LIMIT 1

这会将结果限制为1项

这个

SELECT t1.`from`, t1.item, t1.item 
  FROM t1
WHERE t1.`from` <= NOW()
  GROUP BY item

返回最旧的记录foreach元素而不是nearst到现在。

我应该使用什么?

修改

预期结果(2014-03-31 15:30:00)

|  id  |      from             |  item   |    p    |
    3    2014-03-29 14:30:00       200        48
    5    2014-03-30 23:30:00       500        6

结果我得到:

|  id  |      from             |  item   |    p    |
    1    2014-03-26 08:00:00       500        9
    3    2014-03-29 14:30:00       200        48

服务器时间非常完美!

EDIT2

SELECT t1.`from`, t1.p, prod_desc.name AS prod
  FROM PROD_DESC
  JOIN 
    (SELECT MAX(t1.`from`) `from`, t1.id
       FROM t1
     GROUP BY prod) t2 USING(`from`, id)
  LEFT JOIN t1
    ON t1.item = PROD_DESC.id_prod
WHERE t1.`from` <= NOW()

4 个答案:

答案 0 :(得分:2)

...的DDL

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id  INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,`from`             DATETIME NOT NULL
,item   INT NOT NULL
,p    INT NOT NULL
);

INSERT INTO my_table VALUES
(1,'2014-03-26 08:00:00',500,9),
(2,'2014-03-28 14:30:00',500,7),
(3,'2014-03-29 14:30:00',200,48),
(4,'2014-04-01 19:00:00',200,51),
(5,'2014-03-30 23:30:00',500,6);

SELECT * FROM my_table;
+----+---------------------+------+----+
| id | from                | item | p  |
+----+---------------------+------+----+
|  1 | 2014-03-26 08:00:00 |  500 |  9 |
|  2 | 2014-03-28 14:30:00 |  500 |  7 |
|  3 | 2014-03-29 14:30:00 |  200 | 48 |
|  4 | 2014-04-01 19:00:00 |  200 | 51 |
|  5 | 2014-03-30 23:30:00 |  500 |  6 |
+----+---------------------+------+----+

解决方案:

SELECT x.* 
  FROM my_table x 
  JOIN 
     ( SELECT item
            , MAX(`from`) max_from 
         FROM my_table 
        WHERE `from` <= NOW() 
        GROUP 
           BY item
     ) y 
    ON y.item = x.item 
   AND y.max_from = x.`from`;
+----+---------------------+------+----+
| id | from                | item | p  |
+----+---------------------+------+----+
|  3 | 2014-03-29 14:30:00 |  200 | 48 |
|  5 | 2014-03-30 23:30:00 |  500 |  6 |
+----+---------------------+------+----+

答案 1 :(得分:1)

这可以通过在最大值

上使用自联接来完成
SELECT t.*
FROM t1 t 
JOIN 
(SELECT MAX(t1.`from`) `from`,item ,t1.id
FROM t1 t1
WHERE `from` <= NOW()
GROUP BY item 
) t2 USING(`from`,item)

@Strawberry他对于where子句的位置是正确的,并且连接应该在USING(来自,item)复合条件

答案 2 :(得分:0)

你试过了吗?

...在最后一次更改问题之后,这又是真的:

SELECT t1.`from`, t1.item, t1.item 
FROM t1
WHERE t1.`from` <= NOW()
GROUP BY item
ORDER BY t1.`from` DESC

条件变更后修改

SELECT t1.`from`, t1.item, t1.item 
FROM t1
GROUP BY item
ORDER BY ABS(TIMESTAMPDIFF(MICROSECOND,t1.`from`,NOW()))

再次编辑以进行最终修改

这次我有数据库并检查了结果 - 它们符合您的期望!

SELECT t2.`from`, t2.item, t2.item
FROM ( SELECT * FROM t1 WHERE t1.`from` <= NOW() ORDER BY t1.`from` DESC ) AS t2
GROUP BY t2.item

答案 3 :(得分:-2)

将日期转换为时间戳,然后将它们与当前时间戳的差异进行排序,最后将结果限制为1,这样您只得到最近的一个。