您好我有以下存储过程......
DELIMITER $$
DROP PROCEDURE IF EXISTS `CouponCrusaderDev`.`sp_tblemailcampaignLoadTop4`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_tblemailcampaignLoadTop4`(p_UserID Int(11))
SQL SECURITY INVOKER
BEGIN
SELECT EC.*,(Select MailChimpCampaignID from tblcampaignschedule where EC.CampaignID=tblcampaignschedule.CampaignID AND tblcampaignschedule.MailChimpCampaignID IS NOT NULL) 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$$
DELIMITER ;
我必须显示不为null的MailChinpCampaignID,因此我执行以下代码........
SELECT EC.*,(Select MailChimpCampaignID from tblcampaignschedule where EC.CampaignID=tblcampaignschedule.CampaignID AND tblcampaignschedule.MailChimpCampaignID IS NOT NULL) as MailChimpCampaignID,
但它返回所有null mailchimpcampaignid我能做什么
答案 0 :(得分:0)
您的查询返回null,因为'tblcampaignschedule'中没有匹配id且非null MailChimpCampaignID
的行。我认为最好的办法是内部连接到'tblcampaignschedule'并过滤掉那些空的方式..
这就是我要做的事情(做了一些其他改变以摆脱(我认为)不必要的子选择)。但是,除非您向我们展示您的表结构,并提供一些示例数据,否则最多只能猜测一下。
DELIMITER $$
DROP PROCEDURE IF EXISTS `CouponCrusaderDev`.`sp_tblemailcampaignLoadTop4`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_tblemailcampaignLoadTop4`(p_UserID Int(11))
SQL SECURITY INVOKER
BEGIN
SELECT EC.*,
Select tcs.MailChimpCampaignID as MailChimpCampaignID,
count(distinct tcs.MailChimpCampaignID)as msent,
CT.CampaignType,
max(tcs.ScheduleDateTime) as CampaignDateTime,
Count(*) as Sent
FROM tblemailcampaign EC
inner join tblcampalgntype CT on CT.CampaignTypeID= EC.CampaignTypeID
inner join tblcompanies C On EC.CompanyID = C.CompanyID
inner join tblcampaignschedule tcs ON EC.CampaignID=tcs.CampaignID AND MailChimpCampaignID IS NOT NULL
inner join tblemailsent ES ON tcs.CampaignSceduleID= ES.CampaignSceduleID AND ES.Status= 'ProcessedMessage'
WHERE (p_UserID = 0 OR C.AddUserID = p_UserID)
order by CampaignDateTime desc limit 4
;
END$$
DELIMITER ;
编辑:
DELIMITER $$
DROP PROCEDURE IF EXISTS `CouponCrusaderDev`.`sp_tblemailcampaignLoadTop4`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_tblemailcampaignLoadTop4`(p_UserID Int(11))
SQL SECURITY INVOKER
BEGIN
SELECT EC.*,
Select tcs.MailChimpCampaignID 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
inner join tblcampaignschedule tcs ON EC.CampaignID=tcs.CampaignID AND MailChimpCampaignID IS NOT NULL
WHERE (p_UserID = 0 OR C.AddUserID = p_UserID)
order by CampaignDateTime desc limit 4
;
END$$
DELIMITER ;