开发者

MYSQL Query split by month

开发者 https://www.devze.com 2023-02-15 23:19 出处:网络
At the moment I have a MYSQL query that looks like this... SELECT distinct devid,username FROM auth_log WHERE MONTH(accesstime) = 1;

At the moment I have a MYSQL query that looks like this...

 SELECT distinct devid,username FROM auth_log WHERE MONTH(accesstime) = 1;

This would return all of January which is what I want. Is it possible to return an array or something like that as a single query for a whole year or date range but split by month thus s开发者_JAVA技巧aving me from having to run multiple queries like.

SELECT distinct devid,username FROM auth_log WHERE MONTH(accesstime) = 1;
SELECT distinct devid,username FROM auth_log WHERE MONTH(accesstime) = 2;
SELECT distinct devid,username FROM auth_log WHERE MONTH(accesstime) = 3;


How about just get

SELECT DISTINCT
    MONTH(accesstime) AccessTimeMonth, 
    devid,
    username 
FROM auth_log

and do your filtering work in the front end?


SELECT distinct MONTH(accesstime), devid,username 
   FROM auth_log 
   WHERE MONTH(accesstime) in ( 1, 2, 3 )

or

where month(accesstime) between 1 and

12


SELECT distinct devid, username FROM auth_log WHERE MONTH(accesstime) >= 1 AND MONTH(accesstime) <= 12 ORDER BY MONTH(accesstime); 

Could be cleaned up.. but just one of MANY variations that could be used

0

精彩评论

暂无评论...
验证码 换一张
取 消