让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Id int, -> Name varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(100,'Chris'); mysql> insert into DemoTable values(101,'Bob'); mysql> insert into DemoTable values(102,'David');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable;
这将产生以下输出-
+------+-------+ | Id | Name | +------+-------+ | 100 | Chris | | 101 | Bob | | 102 | David | +------+-------+ 3 rows in set (0.00 sec)
这是创建存储过程以使用WHERE的查询IN()
-
mysql> DELIMITER // mysql> CREATE PROCEDURE whereInDemo(in input varchar(100)) -> BEGIN -> set @Query = 'select Name from DemoTable '; -> set @Query = CONCAT(@Query,' where Id IN (',`input`,')'); -> prepare stmt from @Query; -> execute stmt; -> deallocate prepare stmt; -> END // mysql> DELIMITER ;
现在您可以使用CALL命令调用存储过程-
mysql> call whereInDemo('100,102');
这将产生以下输出-
+-------+ | Name | +-------+ | Chris | | David | +-------+ 2 rows in set (0.04 sec)