首页 > 代码库 > MySQL之常用SQL语句
MySQL之常用SQL语句
1、按小时统计的语句
select
concat(date_format(gmt_create, "%Y-%m-%d %k:00~"), hour(gmt_create)+1, ":00") as ‘time‘,
count(*) as num
from t_order
where gmt_create>=‘2016-03-06 00:00:00‘ and gmt_create<=‘2016-03-06 23:59:59‘
group by left(gmt_create, 13);
select
concat(date_format(gmt_create, "%Y-%m-%d %k:00~"), hour(gmt_create)+1, ":00") as ‘time‘,
count(*) as num
from t_order
where gmt_create>=‘2016-03-06 00:00:00‘ and gmt_create<=‘2016-03-06 23:59:59‘
group by date_format(a.gmt_create,‘%Y-%m-%d %H:00‘);
2、加上序号
select
(@rowNO := @rowNo+1) as rowno,
concat(date_format(a.gmt_create, "%Y-%m-%d %k:00~"), hour(a.gmt_create)+1, ":00") as ‘time‘,
count(*) as num
from t_order a,(select @rowNO :=0) b
where a.gmt_create>=‘2016-03-06 00:00:00‘ and a.gmt_create<=‘2016-03-06 23:59:59‘
group by date_format(a.gmt_create,‘%Y-%m-%d %H:00‘);
3、环比,就是相邻时间段的对比。如:14年4月和14年3月是相邻时间段,这两个时间段的数据对比,就是环比。
select date_format(a.m_adddate,‘%Y-%m‘) as 时间, count(*) as `当月`,
(select count(*) from job_myreceive where date_format(a.m_adddate,‘%Y%m‘) = date_format(date_add(m_adddate,interval 1 month),‘%Y%m‘)) as 上月
from job_myreceive a group by 1
4、同比,是指在相邻时段中的某一相同时间点进行比较。
如:13年和14年是相邻时段,13年3月和14年3月是这两个相邻时段的同一个时间点,都是3月,这两个时段进行数据对比,就是同比。
5、查看数据库的大小,结果是以字节为单位,除1024为K,除1048576为M。
select TABLE_SCHEMA ‘数据库名‘,(sum(DATA_LENGTH)+sum(INDEX_LENGTH))/1048576 ‘大小(M)‘ from information_schema.tables where TABLE_SCHEMA=‘xiancheng‘;
6、查看表的大小,结果是以字节为单位,除1024为K,除1048576为M。
select TABLE_NAME ‘表名‘,(DATA_LENGTH+INDEX_LENGTH)/1048576 ‘大小(M)‘ from information_schema.tables where TABLE_SCHEMA=‘xiancheng‘;
MySQL之常用SQL语句