grade_id
grade_name
price
update_date.
对于给定的成绩,有几个记录,具有不同的日期和时间。价格...
grade_id grade_name price update_date (y-m-d)
1 A 8$ 2011-02-01
1 A 10$ 2011-03-01
1 A 20$ 2011-04-01
2 B 10$ 2011-02-01
2 B 20$ 2011-03-01
2 B 30$ 2011-04-01
如何使用选择查询获取上次更新的价格(但过去) 得到:
1 A 10$ 2011-03-01
2 B 20$ 2011-03-01
因此...(因为最近的价格,过去的日期..
THX 大卫
答案 0 :(得分:4)
SELECT t1.grade_id, t1.grade_name, t1.price, t1.update_date
FROM my_tbl t1
LEFT JOIN my_tbl t2 on t2.grade_id = t1.grade_id
AND t2.update_date > t1.update_date
AND t2.update_date < CURRENT_DATE
WHERE t1.update_date < CURRENT_DATE
AND t2.grade_id IS NULL
ORDER BY t1.grade_name
答案 1 :(得分:1)
root@natasha:test> CREATE TABLE t (grade_id INT UNSIGNED NOT NULL, grade_name CHAR(1) NOT NULL, price CHAR(3) NOT NULL, update_date DATE);
Query OK, 0 rows affected (0.10 sec)
root@natasha:test> INSERT INTO t VALUES (1, 'A', '8$', '2011-02-01'), (1, 'A', '10$', '2011-03-01'), (1, 'A', '20$', '2011-04-01'), (2, 'B', '10$', '2011-02-01'), (2, 'B', '20$', '2011-03-01'), (2, 'B', '30$', '2011-04-01');
Query OK, 6 rows affected (0.13 sec)
Records: 6 Duplicates: 0 Warnings: 0
root@natasha:test> SELECT * FROM t;
+----------+------------+-------+-------------+
| grade_id | grade_name | price | update_date |
+----------+------------+-------+-------------+
| 1 | A | 8$ | 2011-02-01 |
| 1 | A | 10$ | 2011-03-01 |
| 1 | A | 20$ | 2011-04-01 |
| 2 | B | 10$ | 2011-02-01 |
| 2 | B | 20$ | 2011-03-01 |
| 2 | B | 30$ | 2011-04-01 |
+----------+------------+-------+-------------+
6 rows in set (0.00 sec)
root@natasha:test> SELECT * FROM (SELECT * FROM t WHERE update_date < DATE(NOW()) ORDER BY update_date DESC) AS `t` GROUP BY grade_id;
+----------+------------+-------+-------------+
| grade_id | grade_name | price | update_date |
+----------+------------+-------+-------------+
| 1 | A | 10$ | 2011-03-01 |
| 2 | B | 20$ | 2011-03-01 |
+----------+------------+-------+-------------+
2 rows in set (0.00 sec)
答案 2 :(得分:1)
不是大量记录的最快解决方案,而是可读的记录。
select *
from table t1
where update_date =
(select max(update_date)
from table t2
where t2.grade_id = t1.grade_id
and t2.update_date < current_date);
InnoDB表上的(grade_id,update_date)主键有帮助。
答案 3 :(得分:0)
select t1.* from table as t1
inner join (
select grade_id,max(update_date) as update_date
from table where update_date < curdate() group by grade_id ) as t2
on t1.grade_id = t2.grade_id and t1.update_date = t2.update_date
在(grade_id,update_date)
上向表添加索引答案 4 :(得分:-1)
SELECT MAX(update_date) FROM table WHERE DATE(update_date) != DATE(NOW());