MySQL basic query (single table)

MySQL basic query (single table)

Basic format:

select * from table name where condition

1. Use count(*) to query the number

select 车牌,count(*) as count from 小数据1 group by 车牌 ORDER BY count DESC

2. From a column containing date and time

Extract the date with DATE_FORMAT(time,'%Y-%c-%d')

Extract the month with DATE_FORMAT(entry charging time, '%Y-%m') = '2017-08'

select DATE_FORMAT(入口收费时间,'%Y-%m-%d') 起始日期,count(*) 数目 from 小数据1 
WHERE DATE_FORMAT(入口收费时间, '%Y-%m') = '2017-08' group by 起始日期;

or MONTH(DATE_FORMAT(entry charging time, '%Y-%m-%d')) = 8

SELECT * FROM 小数据1 WHERE MONTH(DATE_FORMAT(入口收费时间, '%Y-%m-%d')) = 8 AND DAYOFWEEK(DATE_FORMAT(入口收费时间, '%Y-%m-%d')) = 3;

Extract the date of each day of the month using day(DATE_FORMAT(entry charging time, '%Y-%m-%d'))

select day(DATE_FORMAT(入口收费时间, '%Y-%m-%d'))日期, count(*) 
from 小数据1 WHERE (MONTH(DATE_FORMAT(入口收费时间, '%Y-%m-%d')) = 8) 
group by 日期

Extract the week using DAYOFWEEK(DATE_FORMAT(entry charging time, '%Y-%m-%d'))

SELECT DAYOFWEEK(DATE_FORMAT(入口收费时间, '%Y-%m-%d')) 星期,count(*)交通量 
FROM airportroad_utf8 
WHERE MONTH(DATE_FORMAT(入口收费时间, '%Y-%m-%d')) = 8 GROUP BY 星期

Use DATE_FORMAT (entry charging time, "%H") to extract time

select *,count(*) as 数量 from airportroad_utf8
WHERE (DATE_FORMAT(入口收费时间,"%H")>7 and (DATE_FORMAT(入口收费时间,"%H")<9))
) 

Extract the amount of data at intervals of five minutes

SELECT time, COUNT( * ) AS num 
FROM
	(
	SELECT *,
		DATE_FORMAT(
			concat( date( 入口收费时间 ), ' ', HOUR (入口收费时间 ), ':', floor( MINUTE ( 入口收费时间 ) / 5 ) * 5 ),
			'%Y-%m-%d %H:%i' 
		) AS time 
	FROM airportroad_utf8 where (MONTH(DATE_FORMAT(入口收费时间, '%Y-%m-%d')) = 8) and (起始站点 = '山前') 
)	a 
GROUP BY DATE_FORMAT( time, '%Y-%m-%d %H:%i' ) 
ORDER BY time;

3. To get the speed, you need to calculate (km 1000 3.6/TIME_TO_SEC(TIMEDIFF( 出口收费时间, 入口收费时间))) as speed

4. Get grouped results with group by

5. Use case when to get the result of partition

select 速度段,count(*) as 数量 from(
select (公里*1000*3.6/TIME_TO_SEC(TIMEDIFF(`出口收费时间`,`入口收费时间` ))) as 速度,
case
when 公里*1000*3.6/TIME_TO_SEC(TIMEDIFF(`出口收费时间`,`入口收费时间` )) between 0 and 20 then '0-20'
when 公里*1000*3.6/TIME_TO_SEC(TIMEDIFF(`出口收费时间`,`入口收费时间` )) between 20 and 40 then '20-40'
when 公里*1000*3.6/TIME_TO_SEC(TIMEDIFF(`出口收费时间`,`入口收费时间` )) between 40 and 60 then '40-60'
when 公里*1000*3.6/TIME_TO_SEC(TIMEDIFF(`出口收费时间`,`入口收费时间` )) between 60 and 80 then '60-80'
when 公里*1000*3.6/TIME_TO_SEC(TIMEDIFF(`出口收费时间`,`入口收费时间` )) > 80 then '80+'
end as 速度段
FROM airportroad_utf8
WHERE (MONTH(DATE_FORMAT(入口收费时间, '%Y-%m-%d')) = 8) and  ((DAYOFWEEK(DATE_FORMAT(入口收费时间, '%Y-%m-%d'))=1) or  (DAYOFWEEK(DATE_FORMAT(入口收费时间, '%Y-%m-%d'))=7))
and (DATE_FORMAT(入口收费时间,"%H")>17 and (DATE_FORMAT(入口收费时间,"%H")<19))
)  
a GROUP BY 速度段

When you use case when, you actually create a virtual table and query in this virtual table. Don’t forget the latter a, otherwise an error will be reported.

Guess you like

Origin blog.csdn.net/weixin_43697614/article/details/124117187