Mysql存储过程查询

时间:2011-11-14 11:44:49

标签: mysql

我有以下存储过程,用于返回最近或最后插入的广告系列的已发送电子邮件

“现在我的目标是只显示已发送的广告系列:数量大于”0“我如何更改以下sp来执行此操作,因为现在它将返回所有广告系列,包括”0“和更高版本比“0”“..

代码是......

DELIMITER $$

DROP PROCEDURE IF EXISTS `couponcrusaderdev`.`sp_tblemailcampaignLoadTop4`$$

CREATE DEFINER=`anyone`@`000.000.00.00` PROCEDURE `sp_tblemailcampaignLoadTop4`(p_UserID Int(11))
    SQL SECURITY INVOKER
BEGIN
SELECT EC.*,(Select MailChimpCampaignID from tblcampaignschedule where EC.CampaignID=tblcampaignschedule.CampaignID ) as MailChimpCampaignID,(Select  count(MailChimpCampaignID) from tblcampaignschedule) as msent,CT.CampaignType, (Select tblcampaignschedule.ScheduleDateTime from tblcampaignschedule 
where EC.CampaignID=tblcampaignschedule.CampaignID  order By ScheduleDateTime desc limit 1) as CampaignDateTime,
(Select Count(*) from tblemailsent ES
inner join tblcampaignschedule CS on CS.CampaignSceduleID= ES.CampaignSceduleID
where ES.Status= 'ProcessedMessage' and CS.CampaignID = EC.CampaignID)as Sent
FROM tblemailcampaign EC
inner join tblcampalgntype  CT on CT.CampaignTypeID= EC.CampaignTypeID
inner join tblcompanies C On EC.CompanyID = C.CompanyID
WHERE (p_UserID = 0 OR C.AddUserID = p_UserID)
 order by CampaignDateTime desc limit 4
;
END$$

DELIMI

1 个答案:

答案 0 :(得分:0)

您应该使用 HAVING

 HAVING COUNT(sent) >= 0;

手册:
http://dev.mysql.com/doc/refman/5.0/en/group-by-hidden-columns.html