SQL SERVERÖÐһЩÌرðµØ·½µÄÌرð½â·¨
--1.¹ØÓÚwhereɸѡÆ÷ÖгöÏÖÖ¸¶¨ÐÇÆÚ¼¸µÄÇó½â
SQL code
--»·¾³
create table test_1
(
id int,
value varchar(10),
t_time datetime
)
insert test_1
select 1,'a','2009-04-19' union
select 2,'b','2009-04-20' union
select 3,'c','2009-04-21' union
select 4,'d','2009-04-22' union
select 5,'e','2009-04-23' union
select 6,'f','2009-04-24' union
select 7,'g','2009-04-25'
go
ÎÒÃÇÒ»°ãͨ¹ý datepart(weekday )½øÐÐÇó½â£¬±ÈÈçÇó½âÐÇÆÚ2µÄ¼Ç¼
select * from test_1
where DATEPART(WEEKDAY,t_time+@@DATEFIRST-1)=2
/*
id value t_time
----------- ---------- -----------------------
3 c 2009-04-21 00:00:00.000
*/
ÕâÀïÉæ¼°µ½ @@datefirst Õâ¸öϵͳ±äÁ¿£¬Ò»°ãÎÒÃÇÓÃÀ´µ÷½Ú²»Í¬µØ·½µÄÈÕÆÚÏ°¹ß¡£
Èç¹ûÄã¾õµÃ¹ØÓÚÕâ¸ö±äÁ¿ºÜÄÑÒ²ÀÁµÃÈ¥ÒÀÀµËüµ÷½Ú£¬ÕâÀﻹÓÐÒ»ÖÖ·½·¨
Äã¿ÉÒÔʹÓÃÒ»¸ö²ÎÕÕÈÕÆÚ,ͨ¹ýÏàͬÐÇÆÚÊý³É7µÄ±¶ÊýµÄÔÀí½øÐвéѯ
select * from test_1
where DATEDIFF(DAY,'1900-01-02',t_time)%7=0
/*
id value t_time
----------- ---------- -----------------------
3 c 2009-04-21 00:00:00.000
*/
--2.¹ØÓÚÔÚwhereɸѡÆ÷ÖÐÖ¸¶¨´óСд²éÕÒµÄË÷ÒýÒýÓÃÎÊÌâ
SQL code
--»·¾³
--drop table test_2
create table test_2
(
id int identity(1,1),
value varchar(10)
)
insert test_2 select
'abc' union all select
'Abc' union all select
'ABC' union all select
'aBc'
go
create clustered index in_value on test_2(value)
--ÎÒÏÈÒª²éÕÒ ÖµÎª'ABC'µÄ¼Ç¼ ÒªÇø·Ö´óСдµÄ
select * from test_2
where value COLLATE CHINESE_PRC_CS_AS ='ABC'
°´CTRL+L¿´Ö´Ðмƻ® ·¢ÏÖʱ¾Û¼¯Ë÷ÒýɨÃè Õâ¾Í˵Ã÷Ëü²»ÊÇSARG£¬²»¿¼ÂÇʹÓÃË÷Òý
½â¾ö·½·¨:
select * from test_2
where value COLLATE CHINESE_PRC_CS_AS ='ABC'
and value='ABC'
go
--3.×Ô¶¯È«¾ÖÁÙʱ±í
SQL code ÔÚijЩÇé¿öÏ£¬Äã¿ÉÄÜÐèÒª¿ç»á»°µÄά»¤Ò»Ð©¹²ÏíÖµ£¬ÕâÀï¿ÉÒÔͨ¹ýһЩÊÖ¶Î×Ô¶¯½¨Á¢ÕâÑùÒ»¸öÈ«¾ÖÁÙʱ±í¹»ÄãʹÓÃ
¾ßÌå·½·¨¾ÍÊÇÔÚmasterÊý¾Ý¿âÖн¨Á¢Ò»¸öÒÔsp_¿ªÍ·µÄÌØÊâ´æ´¢¹ý³Ì£¬²¢ÇÒʹÓÃ'startup'±êÖ¾´Ë´æ´¢¹ý³Ì£¬ÕâÑùÿ´ÎÖØÆôÊý¾Ý¿âºó¶¼»á×Ô¶¯ÔËÐд˴洢¹ý³Ì£¬
ͨ¹ýÔÚ´æ´¢¹ý³ÌÖн¨Á¢È«¾ÖÁÙʱ±í£¬¾Í´ïµ½
Ïà¹ØÎĵµ£º
PL/SQLʵÀý·ÖÎö
µÚÎåÕÂ
1¡¢PL/SQLʵÀý·ÖÎö
1£©ÔÚ¡¾SQLPlus Worksheet¡¿ÖÐÖ±½ÓÖ´ÐÐÈçÏÂSQL´úÂëÍê³ÉÉÏÊö²Ù×÷¡£(´´½¨±í)
¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D¨D
CREATE TABLE "SCOTT"."TESTTABLE" ("RECORDNUMBER" NUMBER(4) NOT NULL, "CURRENTDATE" DATE NOT NULL)
TABLESPACE "SYSTEM ......
ÄãÊÇ·ñÔÚǧ·½°Ù¼ÆÓÅ»¯SQL Server Êý¾Ý¿âµÄÐÔÄÜ?Èç¹ûÄãµÄÊý¾Ý¿âÖк¬ÓдóÁ¿µÄ±í¸ñ£¬°ÑÕâЩ±í¸ñ·ÖÇø·ÅÈë¶ÀÁ¢µÄÎļþ×é¿ÉÄÜ»áÈÃÄãÊÜÒæ·Ëdz¡£SQL Server 2005ÒýÈëµÄ±í·ÖÇø¼¼Êõ£¬ÈÃÓû§Äܹ»°ÑÊý¾Ý·ÖÉ¢´æ·Åµ½²»Í¬µÄÎïÀí´ÅÅÌÖУ¬Ìá¸ßÕâЩ´ÅÅ̵IJ¢Ðд¦ÀíÐÔÄÜÒÔÓÅ»¯²éѯÐÔÄÜ¡£
¡¡¡¡SQL ServerÊý¾Ý¿â±í·ÖÇø²Ù×÷¹ý³ÌÓÉÈý¸ö²½Öè×é³É£º
¡ ......
...
)
Óõ½µÄ¹¦ÄÜÓÐ:
1.Èç¹ûÎÒ¸ü¸ÄÁËѧÉúµÄѧºÅ,ÎÒÏ£ÍûËûµÄ½èÊé¼Ç¼ÈÔÈ»ÓëÕâ¸öѧÉúÏà¹Ø(Ò²¾ÍÊÇͬʱ¸ü¸Ä½èÊé¼Ç¼±íµÄѧºÅ);
2.Èç¹û¸ÃѧÉúÒѾ ......
¾¡Á¿ÉÙÓÃIN²Ù×÷·û£¬»ù±¾ÉÏËùÓеÄIN²Ù×÷·û¶¼¿ÉÒÔÓÃEXISTS´úÌæ¡£
²»ÓÃNOT IN²Ù×÷·û£¬¿ÉÒÔÓÃNOT EXISTS»òÕßÍâÁ¬½Ó+Ìæ´ú¡£
OracleÔÚÖ´ÐÐIN×Ó²éѯʱ£¬Ê×ÏÈÖ´ÐÐ×Ó²éѯ£¬½«²éѯ½á¹û·ÅÈëÁÙʱ±íÔÙÖ´ÐÐÖ÷²éѯ¡£¶øEXISTÔòÊÇÊ×Ïȼì²éÖ÷²éѯ£¬È»ºóÔËÐÐ×Ó²éѯֱµ½ÕÒµ½µÚÒ»¸öÆ¥ÅäÏî¡£NOT EXISTS±ÈNOT INЧÂÊÉԸߡ£µ«¾ßÌåÔÚÑ¡ÔñIN»òEXIST² ......
Àý 34 ÕÒ³öÄêÁ䳬¹ýƽ¾ùÄêÁäµÄѧÉúÐÕÃû¡£
SELECT SNAME
from STUDENTS
WHERE AGE £¾
(SELECT AVG(AGE)
from STUDENTS)
Àý 35 ÕÒ³ö¸÷¿Î³ÌµÄƽ¾ù³É¼¨£¬°´¿Î³ÌºÅ·Ö×飬ÇÒֻѡÔñѧÉú³¬¹ý 3 ÈËµÄ¿Î³ÌµÄ³É ......