作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个查询:
SELECT customer.cus_id,customer.fname,customer.lname,customer.image, message.message ,message.msg_id,message.from_cus,message.to_cus, message.sent_time
FROM customer
INNER JOIN message ON cus_id=message.from_cus OR cus_id=message.to_cus
WHERE customer.cus_id IN ( 31,26)
AND (
from_cus =25
OR to_cus = 25
)
AND customer.cus_id NOT IN(25)
ORDER BY message.sent_time DESC
GROUP BY customer.cus_id ;
[Error code:1064 SQL state:42000] 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 'GROUP BY customer.cus_id' at line 11
最佳答案
这不是聚合操作,这是过滤操作。
使用窗口功能:
SELECT cm.* FROM (
SELECT customer.cus_id,customer.fname,customer.lname,customer.image, message.message ,message.msg_id,message.from_cus,message.to_cus, message.sent_time,
ROW_NUMBER() OVER (PARTITION BY customer.cus_id ORDER BY message.sent_time DESC) as seqnum
FROM customer
INNER JOIN message ON cus_id=message.from_cus OR cus_id=message.to_cus
WHERE customer.cus_id IN ( 31,26)
AND (
from_cus =25
OR to_cus = 25
)
AND customer.cus_id NOT IN(25)
) cm
WHERE seqnum = 1
ORDER BY msg_id DESC;
关于mysql - SQL查询中的GROUP BY与Order By一起使用时给出语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61120727/
我是一名优秀的程序员,十分优秀!