mysql 常用查詢語句

2021-07-24 20:56:13 字數 2787 閱讀 2995

1、在原有的時間欄位上做加減

(1)減幾天date_sub

update  bg_outpatient_test set test_time=date_sub(test_time,interval  30 day) where test_time>sysdate();

(2)加幾天adddate

update  bg_outpatient_test set test_time=adddate(test_time,interval  30 day) where test_time>sysdate();

2、按日、周、月統計

select

date_format(create_time,

'%y%u'

) weeks,

count

(caseid) 

count

from

tc_case 

group

byweeks;  

select

date_format(create_time,

'%y%m%d'

) days,

count

(caseid) 

count

from

tc_case 

group

bydays;  

select

date_format(create_time,

'%y%m'

) months,

count

(caseid) 

count

from

tc_case 

group

bymonths;

3、設定乙個時間段的隨機時間戳

set date= concat(floor(2016 + (rand() * 1)),'-',lpad(floor(1 + (rand() * 11)),2,0),'-',lpad(floor(3 + (rand() * 8)),2,0))

set time= concat(lpad(floor(0 + (rand() * 23)),2,0),':',lpad(floor(0 + (rand() * 59)),2,0),':',lpad(floor(0 + (rand() * 59))

update bg_outpatient_test  set test_time= concat(@dates,' ',@time);

4、儲存過程和游標

drop procedure if exists out_patient_test;

create procedure out_patient_test()

begin

declare num int default 0; 

declare ids int default 1001;

declare sn varchar(20);

declare done int ;

declare c_device_relation cursor for select device_sn from patient_device_relation;

/* 異常處理 */

declare continue handler for sqlstate '02000' set done=1;

truncate table bg_outpatient_test;

while num<280 do

open c_device_relation;

set done=0;

while done<>1 do

fetch c_device_relation into sn;

/*rand()取是是乙個0和1之間的隨機小數,floor(x)返回不大於x的最大整數,floor(1.8)等於1*/

/*floor(2016 + (rand() * 1)取2023年,floor是取整函式*/

/*lpad(floor(8 + (rand() * 3)),2,0)取9-11月之間的月份,並且lpad在月份之間填充0,如果大於兩位,則不填充。如9月份則是09,10月是10*/

/*lpad(floor(1+(rand() * 30)),2,0)取1-30號*/

set @dates= concat(floor(2016 + (rand() * 1)),'-',lpad(floor(9 + (rand() * 3)),2,0),'-',lpad(floor(1+(rand() * 30)),2,0));

set @time= concat(lpad(floor(0 + (rand() * 23)),2,0),':',lpad(floor(0 + (rand() * 59)),2,0),':',lpad(floor(0 + (rand() * 59)),2,0));

insert into bg_outpatient_test (id, test_time, code, devicesn, reservedcode, usercode, result, foodstatus, create_time, is_repeat, time_id, time_name, normal_result, normal)

values (ids, concat(@dates,' ',@time), ids, sn, '01', null, '11.41', '2', concat(@dates,' ',@time), '1', 10037, '早餐後', '1', '0');

set ids=ids+1;

end while;

set num=num+1;

close c_device_relation;

end while;

end

MySql常用查詢語句

根據字段進行查詢 select nickname from os user where nickname biubiubiu 查詢os user account表中金額由大到小排名前三的使用者的所有資訊 select from os user account order by balance des...

mysql常用效能查詢語句

檢視mysql本次啟動後的執行時間 單位 秒 show status like uptime 檢視select語句的執行數 show status like com select 檢視insert語句的執行數 show status like com insert 檢視update語句的執行數 sh...

mysql查詢常用小語句

在mysql中有個資料庫information schema下的表tables記錄了所有資料庫中所有的表相關資訊 table schema 資料庫名稱 select count from information schema.tables where table schema 庫名 select c...