我有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"
我试图计算公众或注册用户首次命中文件的日期.
当前的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.
这是因为MIN和MAX上的ifnull()但是如果我不使用ifnull()那么min和max日期都返回NULL.
最后
以上就是激昂泥猴桃最近收集整理的关于mysql查询表里最大的时间_mysql – 在多个表中查找记录的最小和最大日期的全部内容,更多相关mysql查询表里最大的时间_mysql内容请搜索靠谱客的其他文章。
发表评论 取消回复