航空论坛_航空翻译_民航英语翻译_飞行翻译

 找回密码
 注册
搜索
查看: 1859|回复: 0
打印 上一主题 下一主题

经典MYSQL语句大全 [复制链接]

Rank: 9Rank: 9Rank: 9

跳转到指定楼层
1#
发表于 2009-4-5 13:24:44 |只看该作者 |倒序浏览
SQL分类:
2 R6 r* ?' q+ l8 a$ M- T( {DDL―数据定义语言(Create,Alter,Drop,DECLARE)) b/ B0 `3 x9 J9 j
DML―数据操纵语言(Select,Delete,Update,Insert)
9 y: f, q8 u3 D0 d& Y7 WDCL―数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)
7 G5 U) Z- c: ~+ G; t# A+ G( C+ V' Z
2 w8 M) ^' {+ \; Z0 j4 \5 x首先,简要介绍基础语句:1 }7 Q/ D1 ^# h) P& d! M0 `4 s
1、说明:创建数据库) a9 C2 i/ a3 N- ]' M: {4 ~& N( o; h
Create DATABASE database-name
' M# j) x: \- j( v  T  W8 p: U2、说明:删除数据库# O) M7 D* O7 t& M  \, H) ?
drop database dbname
- c1 U& e+ d. ]3、说明:备份sql server+ D- X9 s' z+ K% O- t; b/ Y
--- 创建 备份数据的 device0 ?0 C: q: N) U' B! }8 |- g0 S
USE master
* K5 c! z& T: M4 vEXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'8 Y6 \( B3 X  F+ d! F$ N
--- 开始 备份$ y& j7 Q1 R% u& I. j' P
BACKUP DATABASE pubs TO testBack
, W, E) [2 D: y* P+ d% r4、说明:创建新表& x8 k6 k1 j3 B
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..); ^& @' o! F2 v, ~
根据已有的表创建新表:
* t+ \" t% L) a" H- a' O% @% p, WA:create table tab_new like tab_old (使用旧表创建新表)4 U& ?* ]3 I& W+ F
B:create table tab_new as select col1,col2… from tab_old definition only
0 O" L& Y* N% _! D1 ~+ E5、说明:删除新表) E: g& \; f" H! M0 n, \! h0 F
drop table tabname
) q. w( f+ B1 R4 L3 t0 _2 ^6、说明:增加一个列
5 m$ u( R; M6 D* O/ `0 F& ?Alter table tabname add column col type' g/ z' N! D# |: \% F! x  J
注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。; p$ X0 I' y2 ^' d- R3 O# ?
7、说明:添加主键: Alter table tabname add primary key(col)9 g4 S: }) ]: k! @
说明:删除主键: Alter table tabname drop primary key(col)
. B9 F0 P8 h% [6 B8、说明:创建索引:create [unique] index idxname on tabname(col….)6 U7 D) t, l7 T2 P+ i$ ^
删除索引:drop index idxname/ t( ^, l$ x6 m
注:索引是不可更改的,想更改必须删除重新建。
" @( k: S3 e% w8 A$ e6 m9、说明:创建视图:create view viewname as select statement& T& z& L- x1 W; ?% T
删除视图:drop view viewname5 ]4 U! |  h7 p: H/ X: W+ }
10、说明:几个简单的基本的sql语句/ B$ z) A! F  p4 Z3 F- g
选择:select * from table1 where 范围) z- d( v/ C5 h( X
插入:insert into table1(field1,field2) values(value1,value2)
  L2 c9 O& }" V* S+ u- D7 s删除:delete from table1 where 范围/ Y. ^) u& ~1 ?9 |
更新:update table1 set field1=value1 where 范围
& `7 g4 w  V* m# g* A, J查找:select * from table1 where field1 like ’%value1__’ ; o% j$ x  }; d7 a6 U
排序:select * from table1 order by field1,field2 [desc]
! k8 m. Z& X' k9 Q* l2 L& y总数:select count * as totalcount from table1
6 H- Z& i4 T7 t0 T3 R求和:select sum(field1) as sumvalue from table1
% U0 u" L- i; B% ~/ w3 X5 K平均:select avg(field1) as avgvalue from table1
$ s; c7 B2 C$ h8 n* C7 a# u最大:select max(field1) as maxvalue from table1
, R1 l$ q( ?2 b* V. O9 r, Z最小:select min(field1) as minvalue from table1
3 ?% C+ \  k% h" v# |, e11、说明:几个高级查询运算词
- `2 [: ^0 M, S+ pA: UNION 运算符
2 `% ^2 W" N6 `9 g, TUNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。0 a' O  Q* O6 I1 h
B: EXCEPT 运算符/ U4 A% F! r" D" I4 I. x# h5 B; z
EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。
! j" A& N7 E6 BC: INTERSECT 运算符& v1 j* ?4 p1 k/ _
INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。
9 G; J% T1 G6 o2 x# }6 Z注:使用运算词的几个查询结果行必须是一致的。9 P* k, K9 P7 b9 o$ a5 W4 {

% @; q1 O# G% E8 y3 w# ]2 ~12、说明:使用外连接
% _. |4 P* H: ~6 u6 t' s" @A、left outer join:
$ O9 G9 |% L% m4 C左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。! f# N, @' l" H: N+ b1 o* c
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
) H' h$ V/ ~8 j# s4 CB:right outer join:
9 p2 W  S5 e: `/ n' {3 }- {0 l* B. k右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。& {* c* ]% f/ V5 q8 i) J
C:full outer join:- H0 y% c6 M" ^
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。( X+ r6 e/ f/ b: I1 \
/ w0 T2 m" x* k7 `+ q; Q2 z
其次,大家来看一些不错的sql语句
; h' d; a8 k& J+ E+ Z8 C- n1 `) n1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
9 h7 t0 z! ^9 g0 {# D7 W5 u法一:select * into b from a where 1<>1
$ P' R: ]/ A8 _7 ?: S! r法二:select top 0 * into b from a
% Z9 M7 @; ^$ K1 z  k, E+ ?5 C: [' f! t" N# J$ [) ^
2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)/ ^' x5 T# q2 }  I
insert into b(a, b, c) select d,e,f from b;" k$ ~* B9 k: Y% }
3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
7 I" B0 A  p8 U4 p0 O. o: W% j7 Yinsert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件
4 n1 r6 x$ {0 S8 E# X2 p& ?) p# z例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..
& N0 q0 b1 l- P& q9 |# B2 e) e& D6 K& @7 P, C+ [# ~6 @; \5 \5 B4 k
4、说明:子查询(表名1:a 表名2:b)
; x) N1 p- j' ?" ^7 f  @select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)% Z% W1 s4 S9 J% c# x) L8 ]2 Q
  k/ t! E% h6 J# o: O, [0 F
5、说明:显示文章、提交人和最后回复时间! d* A; e% @3 o5 A; J( v
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
* ~. v. e- g% y: q- U+ ]/ m" C& y. s" @& L2 B3 C
6、说明:外连接查询(表名1:a 表名2:b): w- e; F. h$ Z! ^' W2 h. j$ y! N
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
) t- p. ?+ t* N! K& C: w! K# p: r, I, J( }' v; B
7、说明:在线视图查询(表名1:a )3 ^' \5 j; c% \; r' }: j
select * from (Select a,b,c FROM a) T where t.a > 1;
0 T+ `! \$ s) \5 ]/ R7 K, g& y" L
$ x4 V3 t7 F4 w6 ^' P* M1 r8、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括
6 K5 i' D" [$ v6 Z3 _/ s2 y* A- e  ^8 mselect * from table1 where time between time1 and time22 M9 t+ ?/ M. p/ k2 J+ t- L
select a,b,c, from table1 where a not between 数值1 and 数值28 Z4 Z) A! j! v* S1 r
$ f" e8 T6 }# t, X7 L, O
9、说明:in 的使用方法
: ~; M% ]; f% n! t4 n: uselect * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)& G% I" a' p, o* \
2 ?7 {; \9 Q# r4 F( O, W- o# p
10、说明:两张关联表,删除主表中已经在副表中没有的信息
' d- U* H% A! B( A1 a( @9 O! ndelete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )9 T, C3 A8 s$ [3 H  W) ?6 F! G

' W  |4 W- N# }" G" x11、说明:四表联查问题:; o4 w4 Y7 L% s/ g
select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....# K8 e. t/ z3 @/ b
6 L) w8 i% H8 i
12、说明:日程安排提前五分钟提醒
& l1 L1 n; I6 U: j5 sSQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5) E9 E" t7 x' V( L: h/ ?( n
7 ^; r) i4 g2 i" L
13、说明:一条sql 语句搞定数据库分页
1 i$ Z! ?8 p6 ^select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段  x5 ?% q% w' E9 R2 o* \
4 k! a* }; L0 F) z
14、说明:前10条记录
; Q! O# ]3 }0 q' @7 `select top 10 * from table1 where 范围
4 I2 K$ h( q- x4 g: C
" [/ u2 {5 K; E- K# b0 a; S15、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)6 g  w" m- D4 ?
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b). |& ]* b. r8 M( P8 Y2 X+ }6 C5 v
, Y# E! C+ f9 ^* W
16、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表
# h  P& Z# q. x  q5 b5 E. B(select a from tableA ) except (select a from tableB) except (select a from tableC)
0 c4 f% C4 X: m# t* |3 a2 \/ m- C) X* B$ [
17、说明:随机取出10条数据/ e& D; a/ L3 v( P4 D& l
select top 10 * from tablename order by newid()
* q( R  O1 `2 K* A+ W5 D' I& }$ f; N# {  i$ O' J. B& k
18、说明:随机选择记录
4 q2 _9 @( ^, W; [select newid()& F- p& _1 ]7 i0 B

# F7 }0 Z$ E! i19、说明:删除重复记录
& Y) I; O' J) ~. ~  q6 U# N2 x  WDelete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
: ]3 |, T/ J: y; R: ?: T5 E6 L  S$ P( Q/ y/ q2 L& w& N' [" Q
20、说明:列出数据库里所有的表名
8 N' I$ C" y- E* \select name from sysobjects where type='U'
0 f& u6 y  r3 y) N$ _
: d/ ]8 ?  ]: R# R' `0 Q5 f$ A0 K21、说明:列出表里的所有的3 r* }5 c# E2 A8 a0 [# H; @
select name from syscolumns where id=object_id('TableName'), s# s% f" W) e! N; {* |  [0 T

) ]+ s" X% G- r7 M! R5 W5 D22、说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select 中的case。. P4 Y  z, K5 B0 ^1 l
select type,sum(case vender when 'A' then pcs else 0 end),sum(case vender when 'C' then pcs else 0 end),sum(case vender when 'B' then pcs else 0 end) FROM tablename group by type
/ ?% i! {8 L( I7 `4 }显示结果:8 m) z* U5 U8 k3 Z* L; m
type vender pcs
! Q$ f& ]& n$ i3 M5 s9 _电脑 A 1
4 u! e4 b1 f' V, r5 s7 B, f2 S! T+ p" Y电脑 A 15 X! A' h; {) E6 A
光盘 B 2
9 f, d6 m7 g: [+ H9 K9 T( }光盘 A 2/ n7 D0 I: U% p: e0 O) k# O* z
手机 B 3
& A2 ^% p+ J) U手机 C 3+ ~7 _4 T8 l8 v  n

( Q0 Z1 D& g; t  W* z* K23、说明:初始化表table1
5 m8 ]0 C5 \% ~+ j: D$ o0 oTRUNCATE TABLE table17 ~) P1 x; u. n8 z; s  _

" _7 t6 A$ h+ K1 Z, \8 L( T; _5 u24、说明:选择从10到15的记录
- V: S# y, z  E) o4 J+ i1 Aselect top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc
您需要登录后才可以回帖 登录 | 注册


Archiver|航空论坛 ( 渝ICP备10008336号 )

GMT+8, 2025-12-24 18:06 , Processed in 0.023001 second(s), 10 queries .

Powered by Discuz! X2

© 2001-2011 MinHang.CC.

回顶部