【发布时间】:2010-09-16 19:24:46
【问题描述】:
我有一个这样的示例数据库,其中 id 始终是唯一的,但 user_id 不是唯一的。
id,user_id,message,msg_datetime
111,u1,来自 u1 的消息,这里的时间
112,u2, 来自 u2 的消息,这里的时间
113,u3,来自 u3 的消息,这里有时间
114,u2,来自 u2 的消息,这里的时间
115,u7,来自 u7 的消息,这里有时间
116,u2,来自 u2 的消息,这里的时间
117,u1,来自 u1 的消息,这里的时间
118,u5, 来自 u5 的消息,这里的时间
所以我只想抓住那些通过 msg_datetime 在 DESC 中发送消息的唯一用户。
这是我的查询。select id,DISTINCT user_id,msg_datetime ORDER BY msg_datetime DESC 但我收到错误消息:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT。这里有什么帮助吗?我想要实现的正确语法是什么?
我只想为每个用户显示一个条目,我显示哪个 ID 无关紧要,但每个用户只显示 1 个。
【问题讨论】: