企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
**1. 语法** ```sql IF condition THEN statement_list [ELSEIF condition THEN statement_list] [ELSE statement_list] END IF; - condition:条件成立则执行 statement_list 语句 ``` **2. 案例演示** ```sql # 1. 数据准备 drop table if exists account; create table account ( `id` int(11) primary key auto_increment not null, `username` varchar(255) not null, `password` varchar(255) not null ); insert into account values(1, '张三', 'zhangsan'); insert into account values(2, '李四', 'lisi'); # 2. 在存储过程中使用 IF 语句 delimiter $ create procedure proce_account01(in id_ int, in uname varchar(255), in pass varchar(255)) begin if id_ = 1 then update account set username=uname, password=pass where id=id_; elseif id_ = 2 then delete from account where id=id_; else insert into account(id_, uname, pass); end if; end $ # 3. 调用存储过程 call proce_account01(3, '王五', 'wangwu') $ # 4. 查看结果 mysql> select * from account $ +----+----------+----------+ | id | username | password | +----+----------+----------+ | 1 | 张三 | zhangsan | | 2 | 李四 | lisi | | 3 | 王五 | wangwu | +----+----------+----------+ ```