MySQL之常用SQL語句

來源:互聯網
上載者:User

標籤:strong   where   select   format   sch   同比   相同   mon   大小   

  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語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.