2015-12-24 71 views
1

如果Email被列入黑名單,我希望下面的查詢結果爲Y,否則運行內部查詢並返回特定設施名稱。mysql:內部查詢在selectif語句中不起作用

Select if (EmailBlacklisted = 'Y', 'Y', 
    'select a.facilityname from facility a 
    inner join domainfacility b on a.facility_ID=b.facility_facility_ID 
     inner join domain c on b.domain_Domain_ID = c.Domain_ID 
    where a.facilitystatus="A" and c.DomainName = "cd.com" ') 
from domain where DomainName= 'cd.com'; 

但是查詢返回與RESULT相同的內部查詢,如果emailBlacklisted在域表中是N的話。請幫助

+1

編輯你的問題,並提供樣本數據和預期的結果。 –

回答

1

通常情況下,你只需通過返回工廠名稱接近這個:

select f.facilityname 
from facility f inner join 
    domainfacility df 
    on a.facility_ID = b.facility_facility_ID inner join 
    domain d 
    on df.domain_Domain_ID = d.Domain_ID 
    where f.facilitystatus = 'A' and d.DomainName = 'cd.com' 
where d.DomainName = 'cd.com' and d.EmailBlacklisted <> 'Y'; 

然後,如果這沒有返回,那就表明被列入黑名單。你可以只返回Y這樣的名字:

select (case when d.EmailBlacklisted = 'Y' then 'Y' else f.facilityname end) 
from facility f inner join 
    domainfacility df 
    on a.facility_ID = b.facility_facility_ID inner join 
    domain d 
    on df.domain_Domain_ID = d.Domain_ID 
    where f.facilitystatus = 'A' and d.DomainName = 'cd.com' 
where d.DomainName = 'cd.com'; 

,但似乎與標記混合名稱的混亂得令人不可思議。

0

試試這個:

SELECT IF(d.EmailBlacklisted = 'Y', 'Y', a.facilityname) 
FROM domain d 
LEFT OUTER JOIN domainfacility b ON b.domain_Domain_ID = d.Domain_ID 
LEFT OUTER JOIN facility a ON b.facility_facility_ID = a.facility_ID AND a.facilitystatus = 'A' 
WHERE d.DomainName= 'cd.com';