这是数据库表
╔════╦═════════════╦══════════════════╦═══════╗
║ id ║ customer_id ║ last_seen ║ param ║
╠════╬═════════════╬══════════════════╬═══════╣
║ 1 ║ 12345 ║ 2012-08-01 12:00 ║ 1 ║
║ 2 ║ 22345 ║ 2012-08-01 12:00 ║ 1 ║
║ 3 ║ 32345 ║ 2012-08-01 12:00 ║ 1 ║
║ 4 ║ 42345 ║ 2012-08-01 12:00 ║ 1 ║
║ 5 ║ 52345 ║ 2012-08-01 12:00 ║ 1 ║
║ 6 ║ 12345 ║ 2012-09-01 12:00 ║ 2 ║
║ 7 ║ 12345 ║ 2012-10-01 12:00 ║ 3 ║
╚════╩═════════════╩══════════════════╩═══════╝
其中id
是 AUTO INCREMENT 主键。
我想要实现的是获取每个customer_id
. 预期结果 :
╔════╦═════════════╦══════════════════╦═══════╗
║ id ║ customer_id ║ last_seen ║ param ║
╠════╬═════════════╬══════════════════╬═══════╣
║ 2 ║ 22345 ║ 2012-08-01 12:00 ║ 1 ║
║ 3 ║ 32345 ║ 2012-08-01 12:00 ║ 1 ║
║ 4 ║ 42345 ║ 2012-08-01 12:00 ║ 1 ║
║ 5 ║ 52345 ║ 2012-08-01 12:00 ║ 1 ║
║ 7 ║ 12345 ║ 2012-10-01 12:00 ║ 3 ║
╚════╩═════════════╩══════════════════╩═══════╝
我试过这个 SQL,但它返回不正确的结果:
SELECT customer_id, param, last_seen
FROM `my_table`
GROUP BY customer_id
ORDER BY last_seen DESC
我在这里想念什么?
更新:表结构(输出DESC my_table
)
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| customer_id | varchar(8) | NO | | NULL | |
| last_seen | datetime | NO | | NULL | |
| param | int(11) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+