mysql的max case_MySQL Case Error 1111 - Max(Column) Case Statement in SP

Summary: I am trying to add a Username to the db.username_table with a unique ID incremented by 1. However, I keep receiving an error 1111 related to my CASE statement. It should make the first User_ID 1, then all other new ones the max(user_id) +1. Any help would be greatly appreciated!

Background: This is for my first MySQL project - I have some experience with MS SQL that may be hindering me here. I googled many references and streamlined my code as much as possible, but the aggregate for the counter returns a 1111 error with an IF or with a CASE statement.

DELIMITER $$

CREATE PROCEDURE db.add_user

(

in new_username varchar(45)

)

begin

-- Set Counter ID

declare new_user_id int;

set new_user_id = if(max(db.username_table.User_ID) is null, 1, max(db.username_table.User_ID) + 1);

-- Add Username with Counter

insert into db.username_table (user_id, username)

values (new_user_id, new_username);

END$$

DELIMITER ;

Expected Result - Add a Username to the db.username_table with a unique ID incremented by 1.

Actual Result - Error 1111.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值