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

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

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

Rank: 9Rank: 9Rank: 9

跳转到指定楼层
1#
发表于 2009-4-5 13:24:44 |只看该作者 |倒序浏览
SQL分类:
3 A# t% u7 ]& q. F* f+ Q& I7 v4 x# WDDL―数据定义语言(Create,Alter,Drop,DECLARE)
( s  W. E6 ]& M5 JDML―数据操纵语言(Select,Delete,Update,Insert)8 H5 p8 \" e& w: F
DCL―数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)
2 N0 s" o, G4 X% j2 T( x" e) }5 F2 O5 q* b0 l0 _7 C  m
首先,简要介绍基础语句:
* h9 ^7 [7 P" n+ p1、说明:创建数据库# M/ w' H7 ]' I* x5 z8 L& M
Create DATABASE database-name
' ?7 k" Z& W  M- u2、说明:删除数据库
4 ~2 F4 f# m+ ~7 O" {drop database dbname
+ A8 [4 U! U. k0 y3、说明:备份sql server
; f, R5 s4 q& `5 D4 j5 \--- 创建 备份数据的 device( ~) X! e) k) V+ C3 z% P0 ]
USE master( W% Z0 Y8 _) u/ k4 m
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
" T6 k- x2 L6 y& q2 G8 \# d4 [--- 开始 备份
$ K3 d9 Q) y" y- j! D0 \BACKUP DATABASE pubs TO testBack4 F8 O8 r9 N# P  Q" N. {' r( v
4、说明:创建新表
: H4 A# z: R, _- F3 g- S8 c3 u# [create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..). c/ b$ C/ f& X# g; s' w
根据已有的表创建新表:4 J8 T/ |1 J0 n5 C9 b. j$ W
A:create table tab_new like tab_old (使用旧表创建新表)
% k! ~. M- g- F7 k7 fB:create table tab_new as select col1,col2… from tab_old definition only
( f5 k9 M0 H: N8 R5、说明:删除新表
( ?: N. `& R( A! P4 ~* T2 G' udrop table tabname0 \; {' t5 w* f* x$ n
6、说明:增加一个列* z1 X, W# O  E; O' L5 R
Alter table tabname add column col type' C( S6 d- v( c6 \
注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。  L5 Y1 G/ @2 a. l- p
7、说明:添加主键: Alter table tabname add primary key(col)
; R; k. i2 C# k* G& \/ X说明:删除主键: Alter table tabname drop primary key(col)! Y/ G; Y- W! @% @7 v: G
8、说明:创建索引:create [unique] index idxname on tabname(col….)
, T$ m: H# x, A删除索引:drop index idxname
5 E1 Z1 r9 Q- r. S2 h+ d7 C/ x注:索引是不可更改的,想更改必须删除重新建。
* s9 T2 Y/ Q; |4 k; X6 O$ f9 |9、说明:创建视图:create view viewname as select statement, ?) `8 ^! f# }" E
删除视图:drop view viewname
: @& @9 U: I& B6 h/ @8 K& V10、说明:几个简单的基本的sql语句8 Z) }% q6 J) k' A! w0 Z% |9 [9 I
选择:select * from table1 where 范围( i/ K! ^! H  v) H* H
插入:insert into table1(field1,field2) values(value1,value2)0 z! W# j/ ?& e% b7 U. G
删除:delete from table1 where 范围
$ h- j2 F7 R2 J+ @/ ~0 y% b更新:update table1 set field1=value1 where 范围6 U/ V$ Y  X- n0 ?- O# v
查找:select * from table1 where field1 like ’%value1__’
, s5 k  O% t4 I: J- r排序:select * from table1 order by field1,field2 [desc]
% o4 U1 n* J7 D; p# V+ @( E总数:select count * as totalcount from table12 I7 I& I- Y- Z  W6 h
求和:select sum(field1) as sumvalue from table16 x/ N0 ^$ j! N
平均:select avg(field1) as avgvalue from table1
1 ], W1 I1 m1 c% @6 ^, X0 V8 Y最大:select max(field1) as maxvalue from table1
& l; O6 x2 Z8 V6 H. W% b5 y$ p最小:select min(field1) as minvalue from table1% B$ w; f, ^8 w! y- E, N
11、说明:几个高级查询运算词
1 ^! ~" G' ^% IA: UNION 运算符$ y" D2 w7 u' w$ P0 [
UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。
- B4 w! ^+ H* L* YB: EXCEPT 运算符' b- U! [$ |$ E. y# s' C4 T
EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。# W! U& s0 [$ A9 J" w! c
C: INTERSECT 运算符1 p4 _- y2 K  ?* s0 m
INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。; ?4 P0 [/ o2 R2 k" y) [
注:使用运算词的几个查询结果行必须是一致的。- M9 m% Y0 z, K/ |  W8 t" {: |+ i/ x

' Y& V( h/ B# k9 X. I12、说明:使用外连接" O% V4 q! J: q3 h6 u7 y
A、left outer join:
, c% V  O  s; n" G左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。
$ j, Y5 ^5 J1 R6 G( q* u3 FSQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c  O1 e$ I0 h* `3 ?6 M( f! G
B:right outer join:1 R: {& C+ S9 m! |
右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。
- z+ _0 y  ?3 \, X5 L# \4 eC:full outer join:! W/ d* J0 w' j4 o' I7 x6 O3 d! W
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。3 N! G: e  A! n3 e! ^) H3 u

! h: V' r* T: A8 J' f* }其次,大家来看一些不错的sql语句4 q0 L# J6 i; E1 B+ O, K& j7 U) {
1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
2 t4 F" r/ c! C5 @) F7 K$ W法一:select * into b from a where 1<>1/ {' G/ b: H5 B0 l6 [
法二:select top 0 * into b from a3 `( Z: n" ]. A7 ~- y( C

( k+ E* }+ F: h8 t2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)3 d% G  U1 X9 P) s2 j7 f; z9 V0 j
insert into b(a, b, c) select d,e,f from b;
$ D! Z* F+ ~6 b3 o. `; V$ ~3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
3 N+ T4 r$ E" {& p+ x. Pinsert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件
, Y& C! h, n# e1 B) _. P8 f% a例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..
0 R+ x( F2 c7 N2 O% }6 d. C1 }1 n# H8 C) [
4、说明:子查询(表名1:a 表名2:b)
( p% \4 u  j' K2 f/ w4 pselect a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)/ x! d) q6 w2 b; z/ B/ k
$ [. Y5 a; g7 J+ _' i$ U
5、说明:显示文章、提交人和最后回复时间0 i1 A5 G2 v+ x4 S
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b  H. ^$ Y' A4 p. _
. F: z' _- ^& \( b, c
6、说明:外连接查询(表名1:a 表名2:b), F( R7 P, r: p9 P, e% p1 ~
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
# y' c* ]4 p& m1 ^: `( y9 y, o) }- P2 G& o! C2 G1 ^/ X( x
7、说明:在线视图查询(表名1:a )+ g3 t* c" U7 ?4 c1 [  W$ }
select * from (Select a,b,c FROM a) T where t.a > 1;& x: j3 F+ V8 u
5 }2 [. J! S4 t5 N& ]* I7 f) ]) ~0 Y
8、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括! J2 Y, c7 Y& @) F' `
select * from table1 where time between time1 and time2& f3 V) c1 q0 D' \& d$ J1 ?
select a,b,c, from table1 where a not between 数值1 and 数值2
; |( {' ?; [# ], r0 U5 E
; o" b# V9 G$ y9、说明:in 的使用方法; N4 V& g) s. F7 T0 e8 H' f- i3 |6 L9 E
select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)0 a9 h$ a9 h/ `' [* n7 ~; [" M
8 B8 \+ z. o$ h9 f/ Q
10、说明:两张关联表,删除主表中已经在副表中没有的信息( o% j- A* }7 F, {
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
8 c1 [# e9 o9 `' K! E' }" r! M" I* t/ I# d: P
11、说明:四表联查问题:
8 t2 L: N1 Y* M9 ]; I8 jselect * 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 .....
0 B; v# ^( R9 f1 M4 E: J
, G2 r) j2 i) R1 M12、说明:日程安排提前五分钟提醒7 U4 ?* f- P! b2 W: K
SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5
3 d4 v2 S% [# K3 s4 o
1 n$ V, v6 E4 a13、说明:一条sql 语句搞定数据库分页
: f; [' ]: x, b: W7 uselect top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段2 Z( r) G: N$ P

; q" ?1 g1 w/ c3 F5 e4 G: c14、说明:前10条记录# w' S- b; o8 W- n$ X2 x2 k8 _3 d
select top 10 * from table1 where 范围3 ?# L' l. i' ^1 V
  d6 }: g( |" H, r
15、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)
8 T! Q, W& G5 b! e* Y. Z: _select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)* R& a* y: A8 D) s( w/ C$ k; a: ~

( \' ?0 m/ G+ ^+ W16、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表- m" R+ W/ T% }, S% E/ J, g9 Q
(select a from tableA ) except (select a from tableB) except (select a from tableC)" f1 l0 h0 p- T' I* M

7 P) S, a5 x; B: r17、说明:随机取出10条数据1 b0 k! p$ J' P" b0 l- j8 {
select top 10 * from tablename order by newid()
, f: V7 Z9 m) w6 X9 w  N1 X
" }+ n' s7 Q2 P  V, y0 {' f/ m18、说明:随机选择记录
$ J- r( P' m3 s6 a" B8 n# Kselect newid()
0 I5 X6 Y' H9 O1 J
' X) r8 Y3 }+ L+ o: `19、说明:删除重复记录$ D7 ]# z% W! ?/ s4 _. ]
Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)9 o0 P; X# O3 A* u# c! m+ P: U
3 V. j& x" r$ K5 z: ]/ |
20、说明:列出数据库里所有的表名, y9 _# A; t1 j  |' j
select name from sysobjects where type='U'! ^% O* T# `8 @) r
+ u+ n1 r5 @6 A
21、说明:列出表里的所有的
* j2 D$ j4 S% p% D- mselect name from syscolumns where id=object_id('TableName')
/ D/ Q/ D4 M. t, y0 s5 Y$ r7 w$ a8 L/ s' G2 {& Q
22、说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select 中的case。
1 @8 x* M& g2 Q5 _0 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- |5 S, N5 t# W+ B+ B
显示结果:
3 _( U8 \; l1 G& Dtype vender pcs
0 @* g- R; W2 E; M4 e电脑 A 1
0 ^$ M6 y9 C1 O! V: ^, g/ J+ s3 m: o& H电脑 A 1& c' m6 x' w4 h" y. r% ?( Q# K
光盘 B 2
. A. [1 c4 O& e& \0 ^光盘 A 2, t( J5 X7 `( v+ U. Q! S2 u, B" }
手机 B 3
; p+ p) h& V2 m) J: T, r# _( K7 |手机 C 33 `  C9 F0 I4 g7 K0 K

: Q6 s' f/ Z, _  w; z7 V5 E/ r, A0 g23、说明:初始化表table1
  y: f! x! o- Q! sTRUNCATE TABLE table13 p  w% j8 I2 s6 L& E& N: }

( g3 f1 e# U( Y2 Y! e" a24、说明:选择从10到15的记录
2 |3 I" D' g: @8 X' q6 C  Tselect top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc
您需要登录后才可以回帖 登录 | 注册


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

GMT+8, 2025-10-28 01:32 , Processed in 0.023001 second(s), 10 queries .

Powered by Discuz! X2

© 2001-2011 MinHang.CC.

回顶部