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

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

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

Rank: 9Rank: 9Rank: 9

跳转到指定楼层
1#
发表于 2009-4-5 13:24:44 |只看该作者 |倒序浏览
SQL分类:/ P5 z1 u" ~0 m4 ?
DDL―数据定义语言(Create,Alter,Drop,DECLARE)9 ?9 Y# R/ k  z
DML―数据操纵语言(Select,Delete,Update,Insert)
' \, R5 N9 }# H, R/ F' U( h( y5 hDCL―数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)8 {# l$ {4 J+ W2 j
. w. f6 C+ m9 e9 W9 L4 H) E
首先,简要介绍基础语句:
$ @6 X0 n0 u  R0 W3 i9 t0 m1、说明:创建数据库
/ c6 k8 T) V4 a7 A/ }Create DATABASE database-name
  d! t) k4 w- c3 {" X, Y2、说明:删除数据库" I6 P8 h  F5 R
drop database dbname
  N9 m  [, Q+ J# H, U3、说明:备份sql server) v9 u5 x- y+ L/ j: @& ], o
--- 创建 备份数据的 device
  H" |+ a( m! l5 J$ ?# N; OUSE master
1 Q1 I: V$ D  N7 M' kEXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
" c! n# ?4 h  o5 [' s3 f1 J--- 开始 备份# |$ V, K5 y, V0 z; L8 d* T
BACKUP DATABASE pubs TO testBack
, t, M9 x- U; s3 d! N4、说明:创建新表
/ A) F3 W3 t% ~/ Q# ]create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)3 D7 }5 A3 `. n- k4 j
根据已有的表创建新表:
6 J5 x/ U" U: o9 d6 l$ O( G  J" _A:create table tab_new like tab_old (使用旧表创建新表)
) M4 A, O: }4 T* K1 KB:create table tab_new as select col1,col2… from tab_old definition only
- u0 }( q. a$ [5 ~/ m" i+ D5 ~+ Z5、说明:删除新表
2 N4 F4 ]+ V4 wdrop table tabname
9 o# l0 L! w; n/ B6、说明:增加一个列3 L1 q! P6 c' Q) J, G6 q6 T& o9 t/ y5 ~
Alter table tabname add column col type
8 t+ ?( k$ ~$ u注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。
- O" S- E8 H! N7、说明:添加主键: Alter table tabname add primary key(col)
6 o5 Y7 W8 n+ i说明:删除主键: Alter table tabname drop primary key(col)
$ n: a) u( X1 O) s( U( R( s8、说明:创建索引:create [unique] index idxname on tabname(col….)$ _: e2 [' B. _) s/ j- l0 V8 H
删除索引:drop index idxname6 P8 B3 P; `; Y8 U& K- l* {" W
注:索引是不可更改的,想更改必须删除重新建。
8 p+ N" q8 L. n3 H2 v' `, n9、说明:创建视图:create view viewname as select statement
6 u) S" b: N/ Q- ^$ v! ?删除视图:drop view viewname; E4 x( |/ o* Z) M. B& k
10、说明:几个简单的基本的sql语句# t' ^! }5 ]8 ^
选择:select * from table1 where 范围
( n; A: X: a' r" V4 j- y插入:insert into table1(field1,field2) values(value1,value2): Z% H4 l6 D8 n  t3 d: f4 Z4 Z2 R% B
删除:delete from table1 where 范围
, Q  }! J. B7 Q) b& Q' f更新:update table1 set field1=value1 where 范围
9 ?7 X6 l' Q0 r9 ^查找:select * from table1 where field1 like ’%value1__’ 3 q7 o! \! R& V5 [" J
排序:select * from table1 order by field1,field2 [desc]
) u) X3 w/ d8 q* K+ H总数:select count * as totalcount from table1
. r1 i* @. ^7 Z0 \2 k1 O9 y7 m求和:select sum(field1) as sumvalue from table1" `8 L3 M, l  G! b5 v( T
平均:select avg(field1) as avgvalue from table1
7 g0 l8 x& O  v$ l5 H* m2 \最大:select max(field1) as maxvalue from table1
6 S$ i% W2 ^7 m1 ]7 F3 u' X最小:select min(field1) as minvalue from table1
0 O& ?+ L0 q4 n3 c4 U+ C* p( A11、说明:几个高级查询运算词2 j! s* f! e$ I# R, B* q1 c  C/ C
A: UNION 运算符
9 |. A2 P0 {2 F6 LUNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。
" P7 u5 C# ~, ~9 }2 @! T2 YB: EXCEPT 运算符
9 B  J: w2 i* N2 NEXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。- @1 L: @: d' K, q1 W8 ?
C: INTERSECT 运算符
: T2 w3 J8 `. c7 Z# {INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。: x' [3 r& T$ ?& n. l* [
注:使用运算词的几个查询结果行必须是一致的。
+ e% o* y! [  C1 D: J' }: e. w! ^, H" d0 i8 a
12、说明:使用外连接6 E6 x; M& s3 w
A、left outer join:: p- v! A* D( A# ~" X5 n4 c/ `/ C
左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。% N8 `$ \) R) d
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
- n! Z& d( U0 E" L( L+ C# x/ MB:right outer join:
) q, U; o. r" ]右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。
2 s8 d8 @+ O6 G6 LC:full outer join:
  l$ E* P; f- c, T全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。0 Y3 w8 \) g: d3 p8 {# X

  Y7 {+ E6 x9 Q; ~9 P& e: Q其次,大家来看一些不错的sql语句
5 \+ o+ ]. l, g8 s. i% J1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
/ t* e. m, {) }. \" e0 \法一:select * into b from a where 1<>1
, H: ?1 S9 d, M( h) z法二:select top 0 * into b from a
" d$ X# s# g8 w5 I: S  d' z/ }4 m, A* V
2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)
7 H+ Y: S1 o+ b' h/ p- Jinsert into b(a, b, c) select d,e,f from b;: \. e! s7 t% I  f# A
3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)! B+ X4 O0 K* B, z" f
insert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件
' `, U) J  l: w! i! R例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..1 ~  j$ R- ]' k# p1 y( w: N; w
4 Y, A& K8 e+ \2 T: G; C7 O
4、说明:子查询(表名1:a 表名2:b)
* ^. T) t+ w- }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)5 g7 Y1 `6 f6 G  }; p5 B* c
8 o1 m, v; l, U& @1 o
5、说明:显示文章、提交人和最后回复时间5 P9 ~) ^+ m$ X5 L2 G0 A6 M
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b' x6 e& X+ Y' F. O. V
, _" S  T/ B% d7 o' f9 Y
6、说明:外连接查询(表名1:a 表名2:b); k* {5 ^: C6 S5 W7 U
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
" Z; t" m0 R: J3 s, A9 O( r$ f, D+ M4 W; c& H
7、说明:在线视图查询(表名1:a )  p3 O) N! k+ X7 G0 Z
select * from (Select a,b,c FROM a) T where t.a > 1;' ?1 D; g: z" E3 p, r" d! K

. r7 _5 x# B* {0 Z* L& f$ \$ Q8、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括1 i, z0 i8 ]3 ]1 C8 I
select * from table1 where time between time1 and time2+ D) j' r) Q2 L( N4 K8 ?: ?
select a,b,c, from table1 where a not between 数值1 and 数值2/ h: R! j# F5 f6 v" P6 h& b: f# F
4 B* p" H) E( S. c$ @9 l7 E
9、说明:in 的使用方法
: G4 X4 C- H( S$ |1 tselect * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)
5 Z4 M/ {7 ]- w. n+ k+ S+ u+ S- Q, W) s. h$ v
10、说明:两张关联表,删除主表中已经在副表中没有的信息# F9 O9 T: l# T. a2 i- J) Z
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
5 U1 }8 }, ~4 x. ~1 k7 A2 k" B6 w+ W# }0 p
11、说明:四表联查问题:9 a8 m* o9 u) {6 ?* P
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 .....5 p' y8 x" i# \

6 g& s# q/ @* h! X8 `/ j; ~12、说明:日程安排提前五分钟提醒' Z1 t$ z( h, h9 c: {' w' [
SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5
( ~: I6 w8 m# t  ^5 B1 V0 e7 Z6 L" D7 B) R
13、说明:一条sql 语句搞定数据库分页
: l3 ?; n, {* \9 |2 cselect top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段
  f" z: z" F* S! |# H* q) {2 q
# I6 ]7 J4 w' y% r: d6 e14、说明:前10条记录
' R5 y1 _) y6 `" W6 Yselect top 10 * from table1 where 范围& V0 `% N0 t! U; `1 q& w! x# W7 V5 f: W
/ m7 u9 }9 e3 {) ~6 R* S
15、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)
; o& Q# O  X1 L9 Q1 q5 P$ gselect a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)
2 O" w8 [) ^, E: x! X4 t. a; ]/ L- M1 n4 A6 Q
16、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表
) }4 z) [$ [6 W# l6 i(select a from tableA ) except (select a from tableB) except (select a from tableC)) H- ?6 n: P! W

, I/ s$ e3 S) A0 [' {+ ?17、说明:随机取出10条数据
  E/ C6 a+ H7 Qselect top 10 * from tablename order by newid()+ [& v) ?: E0 Q8 o$ D6 C

( R' r; p4 n4 u. v. R18、说明:随机选择记录: Y! N7 e9 l* y- B' k
select newid(), ?9 C! j# b9 H5 z1 E

% E3 n5 f1 C: x8 f1 R/ f( t19、说明:删除重复记录* T+ Z9 r) c& O  f- U/ v3 A
Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
4 G, m% z8 ?# b* g6 y
/ u4 g- F0 z& @4 Q4 Q: S  V20、说明:列出数据库里所有的表名3 t, O2 w$ Q5 Y1 K9 v; F
select name from sysobjects where type='U'
% P$ N0 z# V4 \3 D  `; g' K1 C( R" s6 H
21、说明:列出表里的所有的' z( P. C( h- X
select name from syscolumns where id=object_id('TableName')& q7 d* r& {" m5 w. h
/ Z9 G5 s# Q" R4 r0 X! V- j
22、说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select 中的case。4 a2 B& b( L- i
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
; I6 `& ?% O! P5 `- a5 y显示结果:
. E4 R; M0 v( ?, c& Btype vender pcs$ y! s8 g+ I' d# |7 y, h: U2 V
电脑 A 1: ~/ y: k% ]  a, ?6 n. t$ {
电脑 A 1
1 I. I% A# C, g" `光盘 B 2$ x$ e" b- s; Y. J( K6 L- h; ~
光盘 A 2
! d$ h" j$ z$ @' Q5 P0 L. k手机 B 3
1 [" M, N! W% ?9 i5 U; T手机 C 3! t$ C* B' Q% q6 E4 _

. V6 @- q% g( ?* ?  s4 ~23、说明:初始化表table1/ r1 i9 u+ F# n% V( i' n
TRUNCATE TABLE table11 q: F! u$ k) ~$ K$ D  e3 R. H

% M6 F; w9 ]# k, X7 H# f( ~24、说明:选择从10到15的记录
; V  {1 b/ H. wselect top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc
您需要登录后才可以回帖 登录 | 注册


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

GMT+8, 2025-12-4 07:10 , Processed in 0.023001 second(s), 10 queries .

Powered by Discuz! X2

© 2001-2011 MinHang.CC.

回顶部