我有这个问题:
SELECT id, rdate, member_id
FROM msgs
WHERE wrote_to='1000'
AND tb1.member_id != '1002'
AND deleted != '1000'
GROUP BY member_id
ORDER BY rdate DESC
问题是我通过rdate获取消息而不是新消息......
我怎样才能通过rdate和group by member来命令它们?...所以我会得到每个用户的最新消息?
感谢。
答案 0 :(得分:0)
rdate
可能来自另一个表,否则您输入了错误的列名。
答案 1 :(得分:0)
尝试:
SELECT * FROM (
SELECT id, rdate, member_id FROM msgs
WHERE wrote_to = '1000'
AND tb1.member_id != '1002'
AND deleted != '1000'
ORDER BY rdate DESC
) tmp_msgs
GROUP BY member_id
由于临时表,这会有一点性能,但这是你必须为一个有效的解决方案做出的折衷。
正如@Brendan Bullen注意到的,为什么你在那里使用别名tb1.
?
答案 2 :(得分:0)
假设一个顺序ID:
SELECT id, rdate, member_id
FROM msgs
WHERE wrote_to='1000'
AND tb1.member_id != '1002'
AND deleted != '1000'
GROUP BY member_id
HAVING id = MAX(id)
ORDER BY rdate DESC
在嵌套选择上使用GROUP BY
不可靠(即使在有序结果上),因为未在组中指定的列的行为未定义。