日期:2014-05-19 浏览次数:20691 次
SELECT b.logindate, count(b.username) totalUser, sum(b.num) totalLogin FROM ( SELECT DATE_FORMAT(a.loginTime,'%Y-%c-%d') logindate, username, COUNT(a.username) num FROM g_user_login_log a GROUP BY logindate, username ) b GROUP BY b.logindate
------解决方案--------------------
查询慢是必然的,因为你这个查询,根本没法用索引,整个过程全都是全表扫描,而且是两轮全表扫描:第一轮是g_user_login_log,第二轮是临时表b。
查不到当月记录这个就显得比较怪异了,似乎没啥道理,或者日期存在问题?
------解决方案--------------------
SELECT a.logindate,b.totalUser ,a.totalLogin FROM (SELECT DATE_FORMAT(a.loginTime,'%Y-%c-%d') logindate, COUNT(a.username) totalLogin FROM g_user_login_log a GROUP BY logindate )a, (SELECT DATE_FORMAT(a.loginTime,'%Y-%c-%d') logindate, COUNT(a.username) totalUser FROM g_user_login_log a GROUP BY logindate ,username)b where a.logindate=b.logindate