MySQL function创建

MySQL创建function示例

DELIMITER //
CREATE FUNCTION `getNewGoods` ()
RETURNS INTEGER
BEGIN
declare count int;
SELECT count(*) into count 
FROM goods gd 
inner join recommend rec on rec.object_id = gd.goods_id 
where 1=1 and info_state='1' and is_del='1' and is_up='0' and rec.type_code = '33';

if count = 0 then
	CREATE TEMPORARY TABLE temp_newgoods SELECT * FROM (
	select tb1.* from (SELECT goods_id,rec.title as goods_name,goods_no,rec.img,list_img 
	FROM goods gd 
	inner join recommend rec on rec.object_id = gd.goods_id 
	where 1=1 and info_state='1' and is_del='1' and is_up='0' and rec.type_code = '33' 
	order by rec.sort) as tb1 
	union 
	select tb2.* from (SELECT goods_id,gd.goods_name,goods_no,list_img as img,list_img 
	FROM goods gd 
	where 1=1 and info_state='1' and is_del='1' and is_up='0' and gd.goods_id not in (
	SELECT goods_id 
	FROM goods gd 
	inner join recommend rec on rec.object_id = gd.goods_id 
	where 1=1 and info_state='1' and is_del='1' and is_up='0' and rec.type_code = '33' )
	order by gd.in_date desc limit 4) as tb2) as goodstable;
elseif count = 1 then 
	CREATE TEMPORARY TABLE temp_newgoods SELECT * FROM (
	select tb1.* from (SELECT goods_id,rec.title as goods_name,goods_no,rec.img,list_img 
	FROM goods gd 
	inner join recommend rec on rec.object_id = gd.goods_id 
	where 1=1 and info_state='1' and is_del='1' and is_up='0' and rec.type_code = '33' 
	order by rec.sort) as tb1 
	union 
	select tb2.* from (SELECT goods_id,gd.goods_name,goods_no,list_img as img,list_img 
	FROM goods gd 
	where 1=1 and info_state='1' and is_del='1' and is_up='0' and gd.goods_id not in (
	SELECT goods_id 
	FROM goods gd 
	inner join recommend rec on rec.object_id = gd.goods_id 
	where 1=1 and info_state='1' and is_del='1' and is_up='0' and rec.type_code = '33' )
	order by gd.in_date desc limit 3) as tb2) as goodstable;
elseif count = 2 then 
	CREATE TEMPORARY TABLE temp_newgoods SELECT * FROM (
	select tb1.* from (SELECT goods_id,rec.title as goods_name,goods_no,rec.img,list_img 
	FROM goods gd 
	inner join recommend rec on rec.object_id = gd.goods_id 
	where 1=1 and info_state='1' and is_del='1' and is_up='0' and rec.type_code = '33' 
	order by rec.sort) as tb1 
	union 
	select tb2.* from (SELECT goods_id,gd.goods_name,goods_no,list_img as img,list_img 
	FROM goods gd 
	where 1=1 and info_state='1' and is_del='1' and is_up='0' and gd.goods_id not in (
	SELECT goods_id 
	FROM goods gd 
	inner join recommend rec on rec.object_id = gd.goods_id 
	where 1=1 and info_state='1' and is_del='1' and is_up='0' and rec.type_code = '33' )
	order by gd.in_date desc limit 2) as tb2) as goodstable;
elseif count = 3 then 
	CREATE TEMPORARY TABLE temp_newgoods SELECT * FROM (
	select tb1.* from (SELECT goods_id,rec.title as goods_name,goods_no,rec.img,list_img 
	FROM goods gd 
	inner join recommend rec on rec.object_id = gd.goods_id 
	where 1=1 and info_state='1' and is_del='1' and is_up='0' and rec.type_code = '33' 
	order by rec.sort) as tb1 
	union 
	select tb2.* from (SELECT goods_id,gd.goods_name,goods_no,list_img as img,list_img 
	FROM goods gd 
	where 1=1 and info_state='1' and is_del='1' and is_up='0' and gd.goods_id not in (
	SELECT goods_id 
	FROM goods gd 
	inner join recommend rec on rec.object_id = gd.goods_id 
	where 1=1 and info_state='1' and is_del='1' and is_up='0' and rec.type_code = '33' )
	order by gd.in_date desc limit 1) as tb2) as goodstable;
else 
	CREATE TEMPORARY TABLE temp_newgoods SELECT * FROM (
	SELECT goods_id,rec.title as goods_name,goods_no,rec.img,list_img 
	FROM goods gd 
	inner join recommend rec on rec.object_id = gd.goods_id 
	where 1=1 and info_state='1' and is_del='1' and is_up='0' and rec.type_code = '33' 
	order by rec.sort) as goodstable;
end if;
RETURN temp_newgoods;
END

创建结束后,直接select进行调用即可。

查看存储过程创建的示例:http://blog.csdn.net/wenxuechaozhe/article/details/51815364

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wenxuechaozhe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值