[email protected]: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)
[email protected]: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
[email protected]: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)
[email protected]: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)
Thwx很多尼克.. – david916 2011-03-19 21:28:41
您的查詢作爲一個魅力...再次感謝 – david916 2011-03-19 21:29:40
不客氣。請將線標記爲已回答。 – 2011-03-19 22:35:11