使用 IN() 仅获取 MySQL 存储过程中的特定记录?

mysqlmysqli database更新于 2024/12/18 6:36:17

首先我们创建一个表 −

mysql> create table DemoTable2004
(
   UserId varchar(20),
   UserName varchar(20)
);
Query OK, 0 rows affected (0.57 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable2004 values('John_123','John');
Query OK, 1 row affected (0.93 sec)
mysql> insert into DemoTable2004 values('23456_Carol','Carol');
Query OK, 1 row affected (0.25 sec)
mysql> insert into DemoTable2004 values('111_Bob','Bob');
Query OK, 1 row affected (0.14 sec)

使用 select 语句显示表中的所有记录 −

mysql> select * from DemoTable2004;

这将产生以下输出 −

+-------------+----------+
| UserId      | UserName |
+-------------+----------+
| John_123    |     John |
| 23456_Carol |    Carol |
| 111_Bob     |      Bob |
+-------------+----------+
3 rows in set (0.00 sec)

下面是创建存储过程并使用 IN() 仅选择特定记录的查询 −

mysql> delimiter //
mysql> create procedure test_of_in(IN uId varchar(20))
   BEGIN
      select * from DemoTable2004 where UserId IN(uId);
   END
   //
Query OK, 0 rows affected (0.15 sec)
mysql> delimiter ;
Call the stored procedure:
mysql> call test_of_in('23456_Carol');

这将产生以下输出 −

+-------------+----------+
| UserId      | UserName |
+-------------+----------+
| 23456_Carol | Carol    |
+-------------+----------+
1 row in set (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.02 sec)

相关文章


有用资源