4
記錄的最小和最大日期我有3個表:查找跨多個表
file_list => list of all files on web server
stats_web => number of pageviews from public web
stats_reg => number of pageviews from "registered users"
我有架構和http://sqlfiddle.com/#!2/98c6a/1/0
我試圖計算日期一些示例數據,一個文件首先被公衆或註冊用戶擊中。
當前SQL是這樣的:
SELECT
list.path,
list.mod_date,
IF(MIN(ifnull(web.hit_date, 0000-00-00)) < MIN(ifnull(reg.hit_date, 0000-00-00)), MIN(ifnull(web.hit_date, 0000-00-00)), MIN(ifnull(reg.hit_date, 0000-00-00))) AS 'min_date',
IF(MAX(ifnull(web.hit_date, 0000-00-00)) > MAX(ifnull(reg.hit_date, 0000-00-00)), MAX(ifnull(web.hit_date, 0000-00-00)), MAX(ifnull(reg.hit_date, 0000-00-00))) AS 'max_date',
SUM(ifnull(web.pages, 0)) + SUM(ifnull(reg.pages, 0)) AS 'page_views'
FROM
file_list list
LEFT JOIN
stats_web web ON list.path = web.path
LEFT JOIN
stats_reg reg ON list.path = reg.path
WHERE
list.path LIKE '/web/htdocs/%'
GROUP BY list.path;
的問題是,如果一個記錄僅出現在統計數據表中的一個最小日期始終爲0 這是因爲IFNULL(中)在MIN和MAX,但如果我不使用ifnull(),則最小和最大日期都會返回NULL。
謝謝dasblinkenlight這個工程很好,但它已經出現了第二個問題。那就是說,如果條目只存在於file_list表中,那麼你會得到'9956'作爲min_date。 – StefWill 2012-07-10 06:04:56