mysql 带参数调用
/********动态查询用户的信息********/
CREATE PROCEDURE GetUsersDynamic(WhereCondition varchar(500),OrderByExpress varchar(100))
begin
declare stmt varchar(2000);
if LENGTH(OrderbyExpress)>0 then
begin
set @sqlstr=concat('select id,name,password,age,getdate(adddate) as AddDate from users where ',WhereCondition,' order by ',OrderByExpress);
end;
else
begin
set @sqlstr=concat('select id,name,password,age,getdate(adddate) as AddDate from users where ',WhereCondition);
end;
end if;
prepare stmt from @sqlstr;
execute stmt;
end;
call GetUsersDynamic('age<=30','');
getdate()是一个自定义的函数,作用是返回日期的短格式
CREATE DEFINER=`root`@`localhost` FUNCTION `getdate`($date datetime) RETURNS varchar(50) CHARSET latin1
return date_format($date,'%Y-%m-%d');
return date_format($date,'%Y-%m-%d');