Django framework used Echart statistical SQL statement

Recent statistics do want Echart graphic display, data source MySQL database is a natural need to filter according to different search criteria the data in the table, with a relatively large number of parameters is the time now!

Conditions commonly used mysql time to retrieve the SQL statement:

Data table: tableName Field Name: myDate 

last 7 days, 
the SELECT  *  the FROM tableName WHERE for DATE_SUB (CURDATE (), the INTERVAL . 7  DAY ) <= DATE (myDate); 

recent one month 
the SELECT  *  the FROM tableName WHERE for DATE_SUB (CURDATE () , INTERVAL 1  MONTH ) <= DATE (myDate); 

last year 
the SELECT  *  the fROM tableName the WHERE DATE_SUB (CURDATE (), INTERVAL 1  YEAR ) <= DATE (myDate); 

week (from Sunday start) 
the SELECT  *  FROM tableName where YEARWEEK(DATE_FORMAT(myDate,'%Y-%m-%d')) = YEARWEEK(now());

上周的
SELECT * FROM tableName where YEARWEEK(DATE_FORMAT(myDate,'%Y-%m-%d')) = YEARWEEK(now())-1;

本月的
SELECT * FROM tableName where date_format(myDate,'%Y-%m')=date_format(now(),'%Y-%m')

上个月的
SELECT * The FROM tableName WHERE DATE_FORMAT (myDate, ' %% Y-m ' ) = DATE_FORMAT (for DATE_SUB (CURDATE (), the INTERVAL . 1  MONTH ), ' %% Y-m ' ); 

from 6-month data is now 
the SELECT  *  the FROM tableName WHERE myDate the BETWEEN DATE_SUB (now (), interval the 6  month the ) and now (); 

this year's data, monthly grouping 
the SELECT  month the (fly_timeStart) AS  month the , COUNT ( * ) the FROMflightinfo.tab_flight_info WHERE  year (fly_timeStart) = 2019  Group  by  month The (fly_timeStart); 

obtaining a first day of the month 
SELECT DATE_ADD (CURDATE (), the INTERVAL - DAY (CURDATE ()) + . 1  Day ); 

Get last month, last day of the 
SELECT for DATE_SUB ( CURDATE (), the INTERVAL DAY (CURDATE ()) + 0  Day );

I want to do is count the last year, the month of data does not include monthly statistics; as well as the month of data, statistics by day.

This is the beginning of the sentence, but it is pushed forward by the current date, I want to push forward from the first day of the month, that does not include the month of data.

Last year data, month packet
 the SELECT plane_type, year (fly_timeStart) AS  year , month The (fly_timeStart) AS  month The , COUNT ( * ) AS  COUNT  
the FROM tab_flight_info WHERE for DATE_SUB (CURDATE (), the INTERVAL 1 YEAR ) <= DATE (fly_timeStart ) the AND plane_type = ' models. 1 '
Group by year (fly_timeStart), month The (fly_timeStart);

Then into the following statement, curdate () changed  for DATE_SUB (CURDATE (), the INTERVAL DAY (CURDATE ()) + Day 0 :

The most recent year of data, monthly grouping
 the SELECT plane_type, year (fly_timeStart) AS  year , month The (fly_timeStart) AS  month The , COUNT ( * ) AS  COUNT  the FROM tab_flight_info the WHERE 
 DATE_SUB (DATE_SUB (CURDATE (), INTERVAL DAY (CURDATE ()) + 0  Day ), the INTERVAL . 1  yEAR ) <= DATE (fly_timeStart) the AND plane_type = ' models. 1 '  
Group by year (fly_timeStart),month(fly_timeStart);

Then the monthly statistics:

Last month's data, grouped by day
 the SELECT  month The (fly_timeStart), Day (fly_timeStart), COUNT ( * ) the FROM flightinfo.tab_flight_info WHERE for DATE_SUB (CURDATE (), the INTERVAL . 1  MONTH ) <= DATE (fly_timeStart) 
 Group  by  month The (fly_timeStart ), Day (fly_timeStart); 

Get data month 
the SELECT  *  from tab_flight_info WHERE the DATE_FORMAT (fly_timeStart, ' %% the Y m ' ) = the DATE_FORMAT (CURDATE (), ' % m% the Y ');
获取当月数据
SELECT month(fly_timeStart) as month,day(fly_timeStart) as day,count(*) as count from tab_flight_info 
where DATE_FORMAT(fly_timeStart,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') AND plane_type='机型1' group by day(fly_timeStart);

 

Ah, first here, describes the next, how to use the front and rear ends of the code Echart Django frame. to be continued!

 

Guess you like

Origin www.cnblogs.com/AndrewYin/p/11265706.html