SQL查詢時間資料和工資評級問題筆記

2021-10-05 14:20:33 字數 3926 閱讀 4223

查詢最近幾天,幾月,幾年的資料

select *

from 表名

where date(列名)>=date_sub(curdate(), interval 3 day)

select *

from 表名

where date(列名)>=date_sub(curdate(), interval 3 month)

select *

from 表名

where date(列名)>=date_sub(curdate(), interval 3 year)

常見工資評級問題

select

case when salary <= 500 then 『1』

when salary > 500 and salary <= 600 then 『2』

when salary > 600 and salary <= 800 then 『3』

when salary > 800 and salary <= 1000 then 『4』

else null end salary_class, – 別名命名

count(*) from table_a

group by

case when salary <= 500 then 『1』

when salary > 500 and salary <= 600 then 『2』

when salary > 600 and salary <= 800 then 『3』

when salary > 800 and salary <= 1000 then 『4』

else null end;

統計12個月份的資料sum(case when)

select

sum(case month(列名) when 『1』 then 1 else 0 end) as 一月份,

sum(case month(列名) when 『2』 then 1 else 0 end) as 二月份,

sum(case month(列名) when 『3』 then 1 else 0 end) as 三月份,

sum(case month(列名) when 『4』 then 1 else 0 end) as 四月份,

sum(case month(列名) when 『5』 then 1 else 0 end) as 五月份,

sum(case month(列名) when 『6』 then 1 else 0 end) as 六月份,

sum(case month(列名) when 『7』 then 1 else 0 end) as 七月份,

sum(case month(列名) when 『8』 then 1 else 0 end) as 八月份,

sum(case month(列名) when 『9』 then 1 else 0 end) as 九月份,

sum(case month(列名) when 『10』 then 1 else 0 end) as 十月份,

sum(case month(列名) when 『11』 then 1 else 0 end) as 十一月份,

sum(case month(列名) when 『12』 then 1 else 0 end) as 十二月份

from 表名

where year(列名)=『2018』;

then 1 ==> 1為常量 相當於count(*)

可以替換為列名 :

sum(case month(列名) when 『1』 then 列名 else 0 end) as 一月份,

分組拼接groupconcat

select group_concat(concat(列名1,列名2)) as 新列名

from 表名

結果如下:

a1,a2,a3

查詢時間資料

1 資料庫欄位pk_time(varchar)

當天的資料

select * from 表 where date(fk_time) = curdate();

當月的資料

select *from 表 where date_format(fk_time,』%y%m』)=date_format(curdate( ),』%y%m』)

昨天select * from 表名 where to_days( now( ) ) - to_days( 時間欄位名) <= 1

7天select * from 表名 where date_sub(curdate(), interval 7 day) <= date(時間欄位名)

近30天

select * from 表名 where date_sub(curdate(), interval 30 day) <= date(時間欄位名)

本月select * from 表名 where date_format( 時間欄位名, 『%y%m』 ) = date_format( curdate( ) , 『%y%m』 )

上一月select * from 表名 where period_diff( date_format( now( ) , 『%y%m』 ) , date_format( 時間欄位名, 『%y%m』 ) ) =1

查詢本季度資料

select * fromht_invoice_informationwhere quarter(create_date)=quarter(now());

查詢上季度資料

select * fromht_invoice_informationwhere quarter(create_date)=quarter(date_sub(now(),interval 1 quarter));

查詢本年資料

select * fromht_invoice_informationwhere year(create_date)=year(now());

查詢上年資料

select * fromht_invoice_informationwhere year(create_date)=year(date_sub(now(),interval 1 year));

查詢當前這週的資料

select name,submittime from enterprise where yearweek(date_format(submittime,』%y-%m-%d』)) = yearweek(now());

查詢上週的資料

select name,submittime from enterprise where yearweek(date_format(submittime,』%y-%m-%d』)) = yearweek(now())-1;

查詢當前月份的資料

select name,submittime from enterprise where date_format(submittime,』%y-%m』)=date_format(now(),』%y-%m』)

查詢距離當前現在6個月的資料

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

2 時間段資料

id 為方法名,parametertype引數用map儲存,resulttype為返回物件 引數tj_start tj_end 提交開始和結束時間

select *from jw_order where 1=1            

and submittime>=#

and submittime<=#

3 模糊查詢sql(mybatis)

select *from 表名 where 1=1

and 字段 like concat(concat(』%』, #), 『%』)

Mysql查詢時間資料

1 查詢當天資料 select from order where to days order time to days now 2 查詢近30天的資料 select from order where date sub curdate interval 30 day date order time 3...

sql 查詢時間段資料

幾個小時內的資料 date sub now interval 5 hour 今天select from 表名 where to days 時間欄位名 to days now 昨天select from 表名 where to days now to days 時間欄位名 1 7天select fro...

sql 多條基本一致的資料中查詢時間最大的資料

現在假設有一張資料表 a 欄位和資料如下 姓名 name 身份證 唯一標識 id 購買產品 pro price 數量 count 購買時間 time 張1111111 computer 1600 52018 03 03 張111111 phone 12 12 2018 03 05 張222222 p...