带group by子句的SQL Update查询 [英] SQL Update query with group by clause

查看:337
本文介绍了带group by子句的SQL Update查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Name         type       Age
-------------------------------
Vijay          1        23
Kumar          2        26
Anand          3        29
Raju           2        23
Babu           1        21
Muthu          3        27
--------------------------------------

编写查询以将每种类型的最大年龄人的名字更新为"HIGH".

Write a query to update the name of maximum age person in each type into 'HIGH'.

还请告诉我,为什么以下查询不起作用

And also please tell me, why the following query is not working

update table1 set name='HIGH' having age = max(age) group by type;

推荐答案

我从Derek更改了脚本,现在它对我有用:

I have changed the script from Derek and it works for me now:

UPDATE table1 AS t 
INNER JOIN 
(SELECT type,max(age) mage FROM table1 GROUP BY type) t1 
ON t.type = t1.type AND t.age = t1.mage 
SET name='HIGH'

这篇关于带group by子句的SQL Update查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆