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)
不正确,但是我不确定如何解决此错误。
感谢您的帮助。
答案 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 by
和limit
:
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
);