SQL UPDATE语句引发错误“无效使用组函数”

时间:2019-05-12 19:22:15

标签: sql

SQL UPDATE语句不断抛出错误

  

无效使用组功能

代码:

UPDATE client
SET car_id = '6', 
    car_name = 'Wedding Limousine', 
    rentdate = '2019-07-27', 
    days = '2', 
    hire_cost_total = '41.99'
WHERE client_id = MAX(client_id);

我认为WHERE client_id = MAX(client_id)不正确,但是我不确定如何解决此错误。

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

您应该使用子查询来获得最大的client_id

UPDATE client
SET car_id = '6'
    , car_name = 'Wedding Limousine'
    , rentdate = '2019-07-27'
    , days = '2'
    , hire_cost_total = '41.99'
WHERE client_id = (
  select MAX(client_id)
  from client 
)

答案 1 :(得分:1)

在MySQL中,您可以使用order bylimit

UPDATE client
    SET car_id = '6',
        car_name = 'Wedding Limousine',
        rentdate = '2019-07-27', 
        days = '2',
        hire_cost_total = '41.99'
    ORDER BY client_id DESC
    LIMIT 1;

答案 2 :(得分:1)

如果要使用最大值client_id更新行:

UPDATE client
SET car_id = '6', car_name = 'Wedding Limousine', rentdate = '2019-07-27', 
days = '2', hire_cost_total = '41.99'    
WHERE client_id = (
  SELECT t.client_id FROM (SELECT MAX(client_id) as client_id FROM client) as t
);