sql语句:
select * from table_name t where t.field1 in (1,2,3,4,...)
当在写存储过程in中的列表用个传入参数代入时,可以使用如下的方式。
以下代码使用find_in_set函数:
复制代码代码示例:
select * from table_name t where find_in_set(t.field1,'1,2,3,4')
另外一个广场,就是组装字符串,然后执行:
复制代码代码示例:
DROP PROCEDURE IF EXISTS photography.Proc_Test
CREATE PROCEDURE photography.`Proc_Test`(param1 varchar(1000))
BEGIN
set @id = param1
set @sel = 'select * from access_record t where t.ID in ('
set @sel_2 = ')'
set @sentence = concat(@sel,@id,@sel_2)-- 连接字符串生成要执行的SQL语句
prepare stmt from @sentence-- 预编释一下。 “stmt”预编释变量的名称,
execute stmt-- 执行SQL语句
deallocate prepare stmt-- 释放
CREATE PROCEDURE test_pro1(IN username VARCHAR(20),IN loginPwd VARCHAR(20))BEGIN
INSERT INTO admin(admin.username,PASSWORD)
VALUES(username,loginpwd)
END $
CREATE PROCEDURE test_pro2(IN id INT,OUT NAME VARCHAR(20),OUT phone VARCHAR(20))
BEGIN
SELECT b.name ,b.phone INTO NAME,phone
FROM beauty b
WHERE b.id = id
END $
CREATE PROCEDURE test_pro3(IN birth1 DATETIME,IN birth2 DATETIME,OUT result INT)
BEGIN
SELECT DATEDIFF(birth1,birth2) INTO result
END $
CREATE PROCEDURE test_pro4(IN mydate DATETIME,OUT strDate VARCHAR(50))
BEGIN
SELECT DATE_FORMAT(mydate,'%y年%m月%d日') INTO strDate
END $
CALL test_pro4(NOW(),@str)
如 传入 :小昭
返回: 小昭 AND 张无忌
DROP PROCEDURE test_pro5 $
CREATE PROCEDURE test_pro5(IN beautyName VARCHAR(20),OUT str VARCHAR(50))
BEGIN
SELECT CONCAT(beautyName,' and ',IFNULL(boyName,'null')) INTO str
FROM boys bo
RIGHT JOIN beauty b ON b.boyfriend_id = bo.id
WHERE b.name=beautyName
END $
CALL test_pro5('柳岩',@str)
DROP PROCEDURE test_pro6
CALL test_pro6(3,5)$
mysql>delimiter //这个作用是把;变成//,以后的语句遇到//就结束了,遇到;不结束,下面就可以按你的想法写了
mysql>CREATE PROCEDURE simpleproc (OUT param1 INT)
->BEGIN
-> SELECT COUNT(*) INTO param1 FROM t
->END
->//
Query OK, 0 rows affected (0.00 sec)
mysql>delimiter 这里把双引号改回来
声明参数要在说明是 输入还是输出函数 in\out
给函数变量赋值用
set @a=10
例子:
drop procedure if exists pr_param_in
create procedure pr_param_in
(
in id int -- in 类型的 MySQL 存储过程参数
)
begin
if (id is not null) then
set id = id + 1
end if
select id as id_inner
end
set @id = 10
call pr_param_in(@id)
select @id as id_out
mysql>call pr_param_in(@id)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)