我有以下 SQL 查询
SELECT
DISTINCT
count("SiteTree_Live"."ID")
FROM
"SiteTree_Live"
LEFT JOIN "Page_Live" ON "Page_Live"."ID" = "SiteTree_Live"."ID"
LEFT JOIN "TourPage_Live" ON "TourPage_Live"."ID" = "SiteTree_Live"."ID"
LEFT JOIN "DepartureDate" ON "DepartureDate"."TourID" = "SiteTree_Live"."ID"
WHERE
("SiteTree_Live"."Locale" = 'en_AU')
AND ("SiteTree_Live"."ClassName" IN ('TourPage'))
AND ("DepartureDate"."DepartureDate" LIKE '2012-11%')
但它产生错误的计数作为查询结果。假设此查询返回的总预期结果不应超过 245,但目前,它返回的结果超过了大约“4569”。
那是因为“DepartureDate”表上的 JOIN,因为当我从“DepartureDate”表中删除连接时,查询返回预期结果。
我需要对查询进行哪些修改以计算“SiteTree_Live”。“ID”和“DepartureDate”。“TourID”之间的 Macthes,而仅计算“SiteTree_Live”。“ID”计数,不包括出发日期?
欢迎任何建议:)
答案
SELECT
COUNT(DISTINCT SiteTree_Live.ID)
FROM
"SiteTree_Live" LEFT JOIN "Page_Live" ON "Page_Live"."ID" = "SiteTree_Live"."ID"
LEFT JOIN "TourPage_Live" ON "TourPage_Live"."ID" = "SiteTree_Live"."ID"
LEFT JOIN "DepartureDate" ON "DepartureDate"."TourID" = "SiteTree_Live"."ID"
WHERE
("SiteTree_Live"."Locale" = 'en_AU')
AND ("SiteTree_Live"."ClassName" IN ('TourPage'))
AND ("DepartureDate"."DepartureDate" LIKE '2013-03%')
似乎给了我正确的结果。感谢@Michael Berkowski 的提示