我在mysql中有这个表:
+----+------+------------+-------+
| id | code | date | value |
+----+------+------------+-------+
| 1 | 3 | 2016-01-01 | 3 |
| 2 | 3 | 2016-01-02 | 4 |
| 3 | 3 | 2016-03-01 | 6 |
| 4 | 3 | 2016-06-06 | 5 |
| 5 | 8 | 2016-01-01 | 1 |
| 6 | 8 | 2016-09-09 | 3 |
+----+------+------------+-------+
我想要返回一个这样的表:
+------+------------+-------+
| code | date | value |
+------+------------+-------+
| 3 | 2016-06-06 | 5 |
| 8 | 2016-09-09 | 3 |
+------+------------+-------+
我的服务器设置是:
**MySQL**
Server: localhost via TCP/IP
Server version: 5.5.16
Protocol version: 10
User: root@localhost
MySQL charset: UTF-8 Unicode (utf8)
**Web server**
Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8
MySQL client version: mysqlnd 5.0.8-dev - 20102224 - $Revision: 310735 $
我尝试过这个脚本而不起作用:
SELECT *
FROM tbl_mysql
WHERE date IN (SELECT MAX(date) FROM tbl_mysql GROUP BY code)
如何更改脚本以使用我的服务器版本? 谢谢!
答案 0 :(得分:2)
答案 1 :(得分:1)
http://sqlfiddle.com/#!9/4cf933/2
SELECT t.*
FROM tbl_mysql t
LEFT JOIN tbl_mysql t2
ON t.code = t2.code
AND t.date<t2.date
WHERE t2.id IS NULL