平常搜集整頓罕見的mysql sql技能。本站提示廣大學習愛好者:(平常搜集整頓罕見的mysql sql技能)文章只能為提供參考,不一定能成為您想要的結果。以下是平常搜集整頓罕見的mysql sql技能正文
空話不多說了,直接給年夜家貼代碼了。
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'