我有以下SQL查詢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」之間的Macthes「TourID」,而僅計算「SiteTree_Live」,不包括出發日期?
任何建議歡迎:)
答案
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%')
似乎給我正確的結果。謝謝你的提示@邁克爾Berkowski
你的意思'COUNT(DISTINCT SiteTree_Live.ID)'代替'DISTINCT COUNT()'? – 2013-04-10 18:52:05
這樣工作嗎?你所有的雙引號應該反引號。 – Barmar 2013-04-10 18:52:58
這個'DepartureDate'是一個字符類型? – wildplasser 2013-04-10 18:54:00