我们可以使用IN运算符创建一个存储过程,以从MySQL表中删除值。为了理解它,我们以一个名为“ student_info”的表为例,该表包含以下数据:
mysql> Select * from student_info; +------+---------+------------+------------+ | id | Name | Address | Subject | +------+---------+------------+------------+ | 100 | Aarav | Delhi | Computers | | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Jaipur | Literature | | 110 | Rahul | Chandigarh | History | +------+---------+------------+------------+ 4 rows in set (0.00 sec)
现在,通过如下创建名为“ delete_studentinfo”的过程,我们可以从“ student_info”表中删除值:
mysql> DELIMITER // ; mysql> Create Procedure Delete_studentinfo ( IN p_id INT) -> BEGIN -> DELETE FROM student_info -> WHERE ID=p_id; -> END // mysql> DELIMITER ; //
现在,使用要从表中删除的值调用过程,如下所示:
mysql> CALL Delete_studentinfo(100); mysql> Select * from student_info; +------+---------+------------+------------+ | id | Name | Address | Subject | +------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Jaipur | Literature | | 110 | Rahul | Chandigarh | History | | 125 | Raman | Bangalore | Computers | +------+---------+------------+------------+ 4 rows in set (0.01 sec)
上面的结果集显示从表中删除了id = 100的记录。