群组功能使用无效
我不断收到这样的声明
群组功能使用无效
此查询
UPDATE users SET users.lastmessage = MAX(messages.id) WHERE users.name ='tom'
我想做的是获取用户表中的lastmessage字段并将其更新为消息表中最大的id,其中名称= tom
我做错了什么?
I keep getting this statement
Invalid use of group function
for this query
UPDATE users SET users.lastmessage = MAX(messages.id) WHERE users.name ='tom'
What I'm trying to do is take the lastmessage field in the users table and update it to be the largest id in the messages table where the name = tom
What did I do incorrectly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要执行子查询来获取用户“tom”的最大 ID,具体操作如下:
编辑:WHERE 子句仅对正确的用户执行此操作
You want to execute a sub-query to get the maximum Id for the user 'tom' which is done as follows:
Edit: WHERE clause to only perform this for the correct user
不能在 GROUP BY 语句之外使用 MAX 函数。您需要在 UPDATE 查询中执行 SELECT 语句才能正确获取该值。例如:
但是请注意,这有一点限制,因为它仅更新 Tom。如果您想更新每个用户,您可以这样做:
这将使用他们发布的最后一条消息的日期更新每个用户。
You cannot use the MAX function outside of a GROUP BY statement. You will need to do a SELECT statement inside your UPDATE query to get this value properly. For example:
Notice, however, that this is a bit limited in that it is only updating Tom. If you wanted to update every user, you could do so like this:
This will update every user with the date of the last message they posted.
我不会说这是解决这个问题的最佳方法,但因为我对你的表结构知之甚少,这似乎正在做你想要的事情。另请记住,我尚未对上述内容进行测试,因此您需要使用自己合适的表和字段来更改表和字段。
I wouldn't say this is the best way to go about solving this, but because I know little about your table structure this seems to be doing what you want. Also keep in mind I haven't tested the above against so you'll need to change the tables and fields with your own proper ones.