日常收集整理常見的mysql sql技巧_Mysql

來源:互聯網
上載者:User

廢話不多說了,直接給大家貼代碼了。

1,數字輔助表

//建立表create table test(id int unsigned not null primary key);delimiter //create procedure pnum(cnt int unsigned)begindeclare i int unsigned default 1;insert into num select i;while i*2 < cnt doinsert into num select i+id from num ;set i=i*2;end while;end//delimiter ;#####列值不連續問題:表a中id值為1,2,3,100,101,110,111set @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_dayfrom(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 nextfrom(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)beginselect floor(days/7)*5+days%7case when 6 between wd and wd+days%7-1 then 1 else 0 endcase then 7 between wd and wd+days%7-1 then 1 else 0 endfrom (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'

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.