³£ÓÃSQL²éѯÓï¾ä

Ê×Ò³ > SQL²éѯ
SQL(Structured Query Language)½á¹¹»¯²éѯÓïÑÔ£¬ÊÇÒ»ÖÖÊý¾Ý¿â²éѯºÍ³ÌÐòÉè¼ÆÓïÑÔ£¬ÓÃÓÚ´æÈ¡Êý¾ÝÒÔ¼°²éѯ¡¢¸üк͹ÜÀí¹ØϵÊý¾Ý¿âϵͳ¡£ÒÔÏÂΪ±¾Õ¾ÊÕ¼¯µÄÔÚÍøÕ¾ÖÆ×÷Öг£Óõ½µÄ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ÀàÐ͵ij¤¶È¡£
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
×¢£ºË÷ÒýÊDz»¿É¸ü¸ÄµÄ£¬Ïë¸ü¸Ä±ØÐëɾ³ýÖØн¨¡£

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'

¶þ¡¢ÌáÉý

1¡¢ËµÃ÷£º¸´ÖƱí(Ö»¸´Öƽṹ,Ô´±íÃû£ºa бíÃû£ºb) (Access¿ÉÓÃ)
·¨Ò»£ºselect * into b from a where 1<>1£¨½öÓÃÓÚSQlServer£©
·¨¶þ£ºselect top 0 * into b from a

2¡¢ËµÃ÷£º¿½±´±í(¿½±´Êý¾Ý,Ô´±íÃû£ºa Ä¿±ê±íÃû£ºb) (Access¿ÉÓÃ)
insert into b(a, b, c) select d,e,f from b;

3¡¢ËµÃ÷£º¿çÊý¾Ý¿âÖ®¼ä±íµÄ¿½±´(¾ßÌåÊý¾ÝʹÓþø¶Ô·¾¶) (Access¿ÉÓÃ)
insert into b(a, b, c) select d,e,f from b in ¡®¾ßÌåÊý¾Ý¿â¡¯ where Ìõ¼þ
Àý×Ó£º..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..

4¡¢ËµÃ÷£º×Ó²éѯ(±íÃû1£ºa ±íÃû2£ºb)
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¡¢ËµÃ÷£ºÏÔʾÎÄÕ¡¢Ìá½»È˺Í×îºó»Ø¸´Ê±¼ä
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

6¡¢ËµÃ÷£ºÍâÁ¬½Ó²éѯ(±íÃû1£ºa ±íÃû2£ºb)
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

7¡¢ËµÃ÷£ºÔÚÏßÊÓͼ²éѯ(±íÃû1£ºa )
select * from (SELECT a,b,c FROM a) T where t.a > 1;

8¡¢ËµÃ÷£ºbetweenµÄÓ÷¨,betweenÏÞÖƲéѯÊý¾Ý·¶Î§Ê±°üÀ¨Á˱߽çÖµ,not between²»°üÀ¨
select * from table1 where time between time1 and time2
select a,b,c, from table1 where a not between ÊýÖµ1 and ÊýÖµ2

9¡¢ËµÃ÷£ºin µÄʹÓ÷½·¨
select * from table1 where a [not] in (¡®Öµ1¡¯,¡¯Öµ2¡¯,¡¯Öµ4¡¯,¡¯Öµ6¡¯)

10¡¢ËµÃ÷£ºÁ½ÕŹØÁª±í£¬É¾³ýÖ÷±íÖÐÒѾ­ÔÚ¸±±íÖÐûÓеÄÐÅÏ¢
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

11¡¢ËµÃ÷£ºËıíÁª²éÎÊÌ⣺
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 .....

12¡¢ËµÃ÷£ºÈճ̰²ÅÅÌáÇ°Îå·ÖÖÓÌáÐÑ
SQL: select * from Èճ̰²ÅÅ where datediff('minute',f¿ªÊ¼Ê±¼ä,getdate())>5

13¡¢ËµÃ÷£ºÒ»Ìõsql Óï¾ä¸ã¶¨Êý¾Ý¿â·ÖÒ³
select top 10 b.* from (select top 20 Ö÷¼ü×Ö¶Î,ÅÅÐò×ֶΠfrom ±íÃû order by ÅÅÐò×ֶΠdesc) a,±íÃû b where b.Ö÷¼ü×ֶΠ= a.Ö÷¼ü×ֶΠorder by a.ÅÅÐò×Ö¶Î
¾ßÌåʵÏÖ£º
¹ØÓÚÊý¾Ý¿â·ÖÒ³£º

declare @start int,@end int

@sql nvarchar(600)

set @sql=¡¯select top¡¯+str(@end-@start+1)+¡¯+from T where rid not in(select top¡¯+str(@str-1)+¡¯Rid from T where Rid>-1)¡¯

exec sp_executesql @sql

×¢Ò⣺ÔÚtopºó²»ÄÜÖ±½Ó¸úÒ»¸ö±äÁ¿£¬ËùÒÔÔÚʵ¼ÊÓ¦ÓÃÖÐÖ»ÓÐÕâÑùµÄ½øÐÐÌØÊâµÄ´¦Àí¡£RidΪһ¸ö±êʶÁУ¬Èç¹ûtopºó»¹ÓоßÌåµÄ×ֶΣ¬ÕâÑù×öÊǷdz£Óкô¦µÄ¡£ÒòΪÕâÑù¿ÉÒÔ±ÜÃâ topµÄ×Ö¶ÎÈç¹ûÊÇÂß¼­Ë÷ÒýµÄ£¬²éѯµÄ½á¹ûºóʵ¼Ê±íÖеIJ»Ò»Ö£¨Âß¼­Ë÷ÒýÖеÄÊý¾ÝÓпÉÄܺÍÊý¾Ý±íÖеIJ»Ò»Ö£¬¶ø²éѯʱÈç¹û´¦ÔÚË÷ÒýÔòÊ×ÏȲéѯË÷Òý£©

14¡¢ËµÃ÷£ºÇ°10Ìõ¼Ç¼
select top 10 * form table1 where ·¶Î§

15¡¢ËµÃ÷£ºÑ¡ÔñÔÚÿһ×ébÖµÏàͬµÄÊý¾ÝÖжÔÓ¦µÄa×î´óµÄ¼Ç¼µÄËùÓÐÐÅÏ¢(ÀàËÆÕâÑùµÄÓ÷¨¿ÉÒÔÓÃÓÚÂÛ̳ÿÔÂÅÅÐаñ,ÿÔÂÈÈÏú²úÆ··ÖÎö,°´¿ÆÄ¿³É¼¨ÅÅÃû,µÈµÈ.)
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)

16¡¢ËµÃ÷£º°üÀ¨ËùÓÐÔÚ TableA Öе«²»ÔÚ TableBºÍTableC ÖеÄÐв¢Ïû³ýËùÓÐÖظ´ÐжøÅÉÉú³öÒ»¸ö½á¹û±í
(select a from tableA ) except (select a from tableB) except (select a from tableC)

17¡¢ËµÃ÷£ºËæ»úÈ¡³ö10ÌõÊý¾Ý
select top 10 * from tablename order by newid()

18¡¢ËµÃ÷£ºËæ»úÑ¡Ôñ¼Ç¼
select newid()

19¡¢ËµÃ÷£ºÉ¾³ýÖظ´¼Ç¼
1),delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
2),select distinct * into temp from tablename
delete from tablename
insert into tablename select * from temp
ÆÀ¼Û£º ÕâÖÖ²Ù×÷Ç£Á¬´óÁ¿µÄÊý¾ÝµÄÒƶ¯£¬ÕâÖÖ×ö·¨²»ÊʺϴóÈÝÁ¿µ«Êý¾Ý²Ù×÷
3),ÀýÈ磺ÔÚÒ»¸öÍⲿ±íÖе¼ÈëÊý¾Ý£¬ÓÉÓÚijЩԭÒòµÚÒ»´ÎÖ»µ¼ÈëÁËÒ»²¿·Ö£¬µ«ºÜÄÑÅжϾßÌåλÖã¬ÕâÑùÖ»ÓÐÔÚÏÂÒ»´ÎÈ«²¿µ¼È룬ÕâÑùÒ²¾Í²úÉúºÃ¶àÖظ´µÄ×ֶΣ¬ÔõÑùɾ³ýÖظ´×Ö¶Î

alter table tablename
--Ìí¼ÓÒ»¸ö×ÔÔöÁÐ
add column_b int identity(1,1)
delete from tablename where column_b not in(
select max(column_b) from tablename group by column1,column2,...)
alter table tablename drop column column_b

20¡¢ËµÃ÷£ºÁгöÊý¾Ý¿âÀïËùÓеıíÃû
select name from sysobjects where type='U' // U´ú±íÓû§

21¡¢ËµÃ÷£ºÁгö±íÀïµÄËùÓеÄÁÐÃû
select name from syscolumns where id=object_id('TableName')

22¡¢ËµÃ÷£ºÁÐʾtype¡¢vender¡¢pcs×ֶΣ¬ÒÔtype×Ö¶ÎÅÅÁУ¬case¿ÉÒÔ·½±ãµØʵÏÖ¶àÖØÑ¡Ôñ£¬ÀàËÆselect ÖеÄcase¡£
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
ÏÔʾ½á¹û£º
type vender pcs
µçÄÔ A 1
µçÄÔ A 1
¹âÅÌ B 2
¹âÅÌ A 2
ÊÖ»ú B 3
ÊÖ»ú C 3


23¡¢ËµÃ÷£º³õʼ»¯±ítable1

TRUNCATE TABLE table1

24¡¢ËµÃ÷£ºÑ¡Ôñ´Ó10µ½15µÄ¼Ç¼
select top 5 * from (select top 15 * from table order by id asc) table_±ðÃû order by id desc

©2008 - 2021 °æȨËùÓÐ 51²éѯÍø

·µ»ØÖ÷Ò³ - 51²éѯ´óÈ«

·µ»Ø¶¥²¿
2021-11-8