我正在尝试获取MySQL查询,以根据参数返回城市页面上的一些活动:
MySQL查询:
SELECT
a.ActivityID as ActivityID,
a.Name as ActivityName,
a.Description as ActivityDescription,
a.Address as ActivityAddress,
a.Mail as ActivityMail,
a.Shorttag as ActivityShorttag,
a.Phone as ActivityPhone,
(SELECT ap.ThumbPath FROM ActivityPictures ap WHERE Acti_ActivityID = a.ActivityID ORDER BY MainPicture DESC, PictureID ASC LIMIT 1) as ActivityThumbPath,
c.CityID as CityID,
c.Name as CityName,
c.PostalCode as CityPostalCode,
c.Shorttag as CityShorttag,
c.ShortDescription as CategoryShortDescription,
c.LongDescription as CategoryLongDescription,
ca.CategoryID as CategoryID
FROM
Cities c
LEFT JOIN
Activities a
ON
c.CityID = a.City_CityID
LEFT JOIN
ActivityCategoryConn acc
ON
a.ActivityID = acc.Acti_ActivityID
LEFT JOIN
Categories ca
ON
acc.Cate_CategoryID = ca.CategoryID
AND
ca.CategoryID IN (2)
WHERE
c.Shorttag = 'city-shorttag'
ORDER BY
a.ActivityID desc
我希望这总是至少返回一行来自" Cities c" - 但如果没有活动与CategoryID匹配,则其余活动只能为NULL。如果有活动,这些当然应该返回匹配的行数。
如何使此查询始终返回至少一条包含c。*的记录 - 即使" IN" -statement中的数组不包含匹配的CategoryID?
答案 0 :(得分:0)
您可以通过使用IS NULL语句进行ORing来回退到空值
AND
(
ca.CategoryID IN (2)
OR
ca.CategoryID IS NULL
)