廢話不多說了,直接給大家貼代碼了。
1,數字輔助表
//創建表 create table test(id int unsigned not null primary key); delimiter // create procedure pnum(cnt int unsigned) begin declare i int unsigned default 1; insert into num select i; while i*2 < cnt do insert into num select i+id from num ; set i=i*2; end while; end // delimiter ; #####列值不連續問題: 表a中id值為1,2,3,100,101,110,111 set @q=0; select id,@q:=@q+1 as cn from a; #####對不連續的進行分組 set @a=0; select min(id) as start_v,max(id) as end_v from ( select id,cn,id-cn as diff from ( select id,@a:=@a+1 as cn from pi) as p ) as pp group by diff; #####對不連續的值填充 use test; DROP TABLE if EXISTS pincer; create table pincer(a int UNSIGNED); insert into pincer values(1),(2),(5),(100),(101),(103),(104),(105); select a+1 as start ,(select min(a)-1 from pincer as ww where ww.a>qq.a) as end from pincer as qq where not exists (select * from pincer as pp where qq.a+1=pp.a) and a<(select max(a) from pincer); ################ select id,num,ranknum,diff from (select id,num,ranknum,num-ranknum as diff from (select id,num,if(@id=id,@rownum:=@rownum+1,@rownum:=1) ranknum,@id:=id from tt,(select @rownum:=0,@id:=null) a ) b) c group by id,diff having count(*)>=2; ################
2,生日問題
select name,birthday,if(cur>today,cur,next) as birth_day from( select name,birthday,today,date_add(cur,interval if(day(birthday)=29 && day(cur)=28,1,0) day)as cur, date_ad(next,interval if(day(birthday)=29 && day(next)=28,1,0) day) as next from( select name,birthday,today, date_add(birthday,interval diff year) as cur, date_add(birthday,interval diff+1 year) as next, from( select concat(laster_name,'',first_name) as name, birth_date as birthday, (year(now())-year(birth_date) )as diff, now() as today from employees) as a ) as b ) as c
3,日期問題----計算工作日
create table sals(id int ,date datetime ,cost int,primary key(id); select date_add('1900-01-01', interval floor(datediff(date,'1900-01-01')/7)*7 day) as week_start, date_add('1900-01-01', interval floor(datediff(date,'1900-01-01')/7*7+6 day) as week_end, sum(cost) from sales; 計算工作日(指定2個日期段 有多少工作日) create procedure pgetworkdays (s datetime,e datetime) begin select floor(days/7)*5+days%7 case when 6 between wd and wd+days%7-1 then 1 else 0 end case then 7 between wd and wd+days%7-1 then 1 else 0 end from (select datediff(e,s)+1 as days,weekday(s)+1 as wd) as a; end;
mysql sql語句大全
1、說明:創建數據庫
CREATE DATABASE database-name
2、說明:刪除數據庫
drop database dbname
3、說明:備份sql server
--- 創建 備份數據的 device
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
--- 開始 備份
BACKUP DATABASE pubs TO testBack
4、說明:創建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
根據已有的表創建新表:
A:create table tab_new like tab_old (使用舊表創建新表)
B:create table tab_new as select col1,col2… from tab_old definition only
5、說明:刪除新表
drop table tabname
6、說明:增加一個列
Alter table tabname add column col type
注:列增加後將不能刪除。DB2中列加上後數據類型也不能改變,唯一能改變的是增加varchar類型的長度。
7、說明:添加主鍵: Alter table tabname add primary key(col)
說明:刪除主鍵: Alter table tabname drop primary key(col)
8、說明:創建索引:create [unique] index idxname on tabname(col….)
刪除索引:drop index idxname
注:索引是不可更改的,想更改必須刪除重新建。
9、說明:創建視圖:create view viewname as select statement
刪除視圖:drop view viewname
10、說明:幾個簡單的基本的sql語句
選擇:select * from table1 where 范圍
插入:insert into table1(field1,field2) values(value1,value2)
刪除:delete from table1 where 范圍
更新:update table1 set field1=value1 where 范圍
查找:select * from table1 where field1 like '%value1%' ---like的語法很精妙,查資料!
排序:select * from table1 order by field1,field2 [desc]
總數:select count as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1
11、說明:幾個高級查詢運算詞
A: UNION 運算符
UNION 運算符通過組合其他兩個結果表(例如 TABLE1 和 TABLE2)並消去表中任何重復行而派生出一個結果表。當 ALL 隨 UNION 一起使用時(即 UNION ALL),不消除重復行。兩種情況下,派生表的每一行不是來自 TABLE1 就是來自 TABLE2。
B: EXCEPT 運算符
EXCEPT 運算符通過包括所有在 TABLE1 中但不在 TABLE2 中的行並消除所有重復行而派生出一個結果表。當 ALL 隨 EXCEPT 一起使用時 (EXCEPT ALL),不消除重復行。
C: INTERSECT 運算符
INTERSECT 運算符通過只包括 TABLE1 和 TABLE2 中都有的行並消除所有重復行而派生出一個結果表。當 ALL 隨 INTERSECT 一起使用時 (INTERSECT ALL),不消除重復行。
注:使用運算詞的幾個查詢結果行必須是一致的。
12、說明:使用外連接
A、left (outer) join:
左外連接(左連接):結果集幾包括連接表的匹配行,也包括左連接表的所有行。
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
B:right (outer) join:
右外連接(右連接):結果集既包括連接表的匹配連接行,也包括右連接表的所有行。
C:full/cross (outer) join:
全外連接:不僅包括符號連接表的匹配行,還包括兩個連接表中的所有記錄。
12、分組:Group by:
一張表,一旦分組完成後,查詢後只能得到組相關的信息。
組相關的信息:(統計信息) count,sum,max,min,avg 分組的標准)
在SQLServer中分組時:不能以text,ntext,image類型的字段作為分組依據
在selecte統計函數中的字段,不能和普通的字段放在一起;
13、對數據庫進行操作:
分離數據庫: sp_detach_db; 附加數據庫:sp_attach_db 後接表明,附加需要完整的路徑名
14.如何修改數據庫的名稱:
sp_renamedb 'old_name', 'new_name'