在mysql过程中处理null参数 - 不工作

时间:2010-10-01 09:44:08

标签: mysql null procedure

我将null传递给下面的过程如下所示,即使表中有数据,它也不返回任何值?

调用view_ads(NULL,'vehicles',9);

调用view_ads('placename',null,9);

=============================================== ======================================

下面是代码

delimiter $$
drop procedure view_ads$$
create procedure view_ads (place_name varchar(200),category_name varchar(200),pagecount int(8))
begin
declare lowerval int(8);
declare higherval int(8);
set lowerval= pagecount-9;
set higherval=pagecount;
if isnull(place_name) then

SET @sql = CONCAT( "select idads_list,category,subcategory,state,city,title,
      title_decode,phonenumber,userid,description,DATE_FORMAT(ad_date,'%d-%M-%Y %H:%i:%s') posteddate,automated
       from ads_list
where automated='N' and ( category ='",category_name,"' or subcategory='",category_name,"')
union all
select idads_list,category,subcategory,state,city,title,
      title_decode,phonenumber,userid,description,DATE_FORMAT(ad_date,'%d-%M-%Y %H:%i:%s') posteddate,automated
       from ads_list
where automated='Y' and  ( category ='",category_name,"' or subcategory='",category_name,"')
order by automated asc,posteddate  desc
limit ",lowerval,",",higherval)
;
elseif ISNULL(category_name) then
SET @sql = CONCAT( "select idads_list,category,subcategory,state,city,title,
      title_decode,phonenumber,userid,description,DATE_FORMAT(ad_date,'%d-%M-%Y %H:%i:%s') posteddate,automated
       from ads_list
where automated='N' and ( state ='",place_name,"' or city='",place_name,"')
union all
select idads_list,category,subcategory,state,city,title,
      title_decode,phonenumber,userid,description,DATE_FORMAT(ad_date,'%d-%M-%Y %H:%i:%s') posteddate,automated
       from ads_list
where automated='Y' and  ( state ='",place_name,"' or city='",place_name,"')
order by automated asc,posteddate  desc
limit ",lowerval,",",higherval)
;
else
SET @sql = CONCAT( "select idads_list,category,subcategory,state,city,title,
      title_decode,phonenumber,userid,description,DATE_FORMAT(ad_date,'%d-%M-%Y %H:%i:%s') posteddate,automated
       from ads_list
where automated='N' and ( state ='",place_name,"' or city='",place_name,"') and  ( category ='",category_name,"' or subcategory='",category_name,"')
union all
select idads_list,category,subcategory,state,city,title,
      title_decode,phonenumber,userid,description,DATE_FORMAT(ad_date,'%d-%M-%Y %H:%i:%s') posteddate,automated
       from ads_list
where automated='Y' and  ( state ='",place_name,"' or city='",place_name,"') and  ( category ='",category_name,"' or subcategory='",category_name,"')
order by automated asc,posteddate  desc
limit ",lowerval,",",higherval)
;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt; 
end if;
end$$

1 个答案:

答案 0 :(得分:0)

尝试与空字符串进行比较,如下所示:

delimiter $$
drop procedure view_ads$$
create procedure view_ads (place_name varchar(200),category_name varchar(200),pagecount int(8))
begin
declare lowerval int(8);
declare higherval int(8);
set lowerval= pagecount-9;
set higherval=pagecount;

if place_name = '' then
SET @condition = CONCAT("and ( category ='",category_name,"' or subcategory='",category_name,"')");
else if category_name = '' then
SET @condition = CONCAT("( state ='",place_name,"' or city='",place_name,"')");
else
SET @condition = CONCAT("( state ='",place_name,"' or city='",place_name,"') and  ( category ='",category_name,"' or subcategory='",category_name,"')");

SET @SQL = CONCAT( "select idads_list,category,subcategory,state,city,title,
      title_decode,phonenumber,userid,description,DATE_FORMAT(ad_date,'%d-%M-%Y %H:%i:%s') posteddate,automated
       from ads_list
where automated='N' ",condition,"
union all
select idads_list,category,subcategory,state,city,title,
      title_decode,phonenumber,userid,description,DATE_FORMAT(ad_date,'%d-%M-%Y %H:%i:%s') posteddate,automated
       from ads_list
where automated='Y' ",condition,"
order by automated asc,posteddate  desc
limit ",lowerval,",",higherval)
;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt; 
end if;
end$$

此外,我对你的程序进行了一些修改,使其更短。