MySQL資料庫查詢語句記錄學習

2021-09-10 08:16:09 字數 1928 閱讀 7533

以上圖資料庫表為例,表名moncenter_data_air,表結構如下圖

1.查詢今天資料指令

select *, datatime from moncenter_data_air where to_days(datatime) = to_days(now());

2.查詢昨天資料指令

select *, datatime from moncenter_data_air where to_days(now()) - to_days(datatime) <= 1;

3.查詢近7天資料指令 

select *, datatime from moncenter_data_air where date_sub(curdate(),interval 7 day) <= date(datatime);

4.查詢近30天資料指令    

select *, datatime from moncenter_data_air where date_sub(curdate(), interval 30 day) <= date(datatime); 

5.查詢本月資料指令    

select *, datatime from moncenter_data_air where date_format(datatime, '%y%m') = date_format(curdate(), '%y%m');  

6.查詢上月資料指令    

select *, datatime from moncenter_data_air where period_diff(date_format(now(), '%y%m'), date_format(datatime, '%y%m')) = 1;

7.查詢本季度資料指令

select *, datatime from moncenter_data_air where quarter(datatime) = quarter(now());

8.查詢上季度資料指令

select *, datatime from moncenter_data_air where quarter(datatime) = quarter(date_sub(now(), interval 1 quarter));

9.查詢本年資料指令

select *, datatime from moncenter_data_air where year(datatime) = year(now());

10.查詢上年資料指令

select *, datatime from moncenter_data_air where year(datatime) = year(date_sub(now(),interval 1 year));

11.查詢距離當前現在6個月的資料指令

select *, datatime from moncenter_data_air where datatime between date_sub(now(), interval 6 month) and now();

12.查詢當前這週的資料指令

select *, datatime from moncenter_data_air where yearweek(date_format(datatime, '%y-%m-%d')) = yearweek(now());

13.查詢上週的資料指令

select *, datatime from moncenter_data_air where yearweek(date_format(datatime, '%y-%m-%d')) = yearweek(now()) - 1;

mysql 分頁查詢語句資料庫查詢

sql server 關於分頁 sql 的資料許多,有的使用儲存過程,有的使用游標。本人不喜歡使用游標,我覺得它耗資 效率低 使用儲存過程是個不錯的選擇,因為儲存過程是顛末預編譯的,執行效率高,也更靈活。先看看單條 sql 語句的分頁 sql 吧。方法1 適用於 sql server 2000 20...

資料庫查詢語句

表示要查詢所有列 select from t student 查詢所有人的姓名資訊 select stu name from t student 查詢張乾的手機號和位址 select stu mobile,stu address from t student where stu name 張乾 查詢...

資料庫 查詢語句

隱式連線 select coursename,teachername from courses inner join teachers on courses.teacherid teahcer.teacherid 等價於顯式連線 select coursename,teachername from ...