Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

SQLÏÂÈÕÆÚº¯ÊýʹÓÃ

ÕâÁ½Ììдһ¸öºÍͳ¼ÆÊý¾ÝÓйصĴ洢¹ý³Ì£¬ÀïÃæÒªÀûÓÃÈÕÆÚ½øÐÐһЩ¼ÆËãºÍÅжϣ¬Ò²×ÔȻҪÀûÓÃSQLµÄһЩÈÕÆÚÏà¹ØµÄº¯Êý¡£ÕâÀïÂÔ¼Çһϣ¬µ±ÊǸ´Ï°Ò»ÏÂSQL¡£
ÀûÓÃSQL½Å±¾ÄÚÖõļ¸¸öº¯Êý£¬ÎÒÃÇÄÜÁé»îµÄ¶ÔÈÕÆÚ½øÐмÆËãºÍ±È½Ï¡£³£Óõļ¸¸öº¯Êý£ºGETDATE(),DATEDIFF(),DATEADD()
GETDATE() µ±È»¹ËÃû˼Ò壬µÃµ½µ±Ç°µÄÈÕÆÚ£¬·µ»ØÀàÐÍÊÇDateTimeÀàÐÍ¡£
DATEDIFF ( datepart , startdate , enddate ) ÓÃÓÚÅжÏÔÚÁ½¸öÈÕÆÚÖ®¼ä´æÔÚµÄÖ¸¶¨Ê±¼ä¼ä¸ôµÄÊýÄ¿¡£
µÚÒ»¸ö²ÎÊýÊÇÖ¸¶¨Ê±¼ä¼ä¸ôµÄÀàÐÍ,ÀýÈçmm(ÔÂ),dd(Ìì)£¬yy(Äê),ms(ºÁÃ룩,ss(Ã룩£¬²»Í¬µÄ¼ä¸ôÀàÐÍ£¬·µ»ØµÄ½á¹ûÒ²²»Ò»Ñù¡£
DATEADD (datepart , number, date ) ÓÃÓÚÈÕÆÚÔËËãµÄº¯Êý£¬½«´«ÈëµÄÈÕÆÚ£¬¼ÓÉÏÖ¸¶¨Ê±¼ä¼ä¸ôÊýÄ¿µÄÈÕÆÚ¡£
ÀýÈ磬¼ÆËãµÃµ½±¾ÄêµÄµÚÒ»Ì죺
Select DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
ÎÒÃÇÀ´·ÖÎöÏÂÕâ¸öSQLÓï¾ä¾Í¿ÉÒÔÖªµÀ£¬Ê×ÏÈ´Ó×îÀïÃæµÄgetdate()¿ªÊ¼£¬getdate()µÃµ½µ±Ç°ÈÕÆÚºÍʱ¼ä£¬Íâ²ãµÄdatediff£¬¼ÆË㵱ǰÈÕÆÚºÍ1900-01-01 00:00:00Ö®¼äµÄʱ¼ä¼ä¸ô£¬·µ»Øµ¥Î»ÊÇÒÔÄêÀ´Í³¼ÆµÄ£¬Èç¹ûÎÒÃÇ·Ö¿ª¿´DATEDIFF(yy,0,getdate()),0)µÄ½á¹û£¬·µ»Ø¾ÍÊÇ:109¡£·µ»ØµÄ109£¬´«µÝ¸ø×îÍâ²ãµÄDATEADDº¯Êý£¬½«1900-01-01 00£º00£º00¼ÓÉÏ109Ä꣬µÃµ½µÄ½á¹û£¬×ÔÈ»¾ÍÊÇ2009-01-01 00:00:00ÁË£¬Ò²¼´±¾ÄêµÄµÚÒ»Ìì¡£
ͬÑù£¬Áé»îµÄÀûÓÃÕ⼸¸öº¯ÊýµÄ×éºÏ£¬ÎÒÃÇ¿ÉÒԵõ½²»Í¬µÄ½á¹û£º
µÃµ½µ±ÔµĵÚÒ»Ì죺Select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
µÃµ½µ±Ç°¼¾¶ÈµÄµÚÒ»Ì죺Select DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)
µÃµ½µ±ÌìµÄÆðʼʱ¼ä£º Select DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)
µÃµ½ÉϸöÔÂ×îºóÒ»Ì죺 Select DATEADD(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))
µÃµ½ÉϸöÔµĵÚÒ»Ì죺 Select DATEADD(m,-1,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))
ÆäÔ­Àí¾ÍÊǵõ½µ±ÔµÚÒ»Ì죬ÔÙ¼õÈ¥ÈýºÁÃë(SQLµÄʱ¼äÒÔ3ºÁÃëΪһ¸öµ¥Î»)£¬ÕâÑùÒÔµ±Ç°Îª2Ô£¬µÃµ½µÄ½á¹û¾ÍÊÇ£º2009-01-31 23:59:59.997
µÃµ½È¥ÄêµÄ×îºóÒ»Ì죺 Select DATEADD(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))
µÃµ½±¾ÔµÄ×îºóÒ»Ì죺 Select DATEADD(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))
µÃµ½±¾ÄêµÄ×îºóÒ»Ì죺 Select DATEADD(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0))
µÃµ½±¾ÔµĵÚÒ»¸öÐÇÆÚÒ»£º Select DATEADD(wk, DATEDIFF(wk,0,DATEADD(dd,6-datepart(day,getdate()),g


Ïà¹ØÎĵµ£º

SQLÖظ´¼Ç¼²éѯ

SQLÖظ´¼Ç¼²éѯ
1¡¢²éÕÒ±íÖжàÓàµÄÖظ´¼Ç¼£¬Öظ´¼Ç¼ÊǸù¾Ýµ¥¸ö×ֶΣ¨peopleId£©À´ÅжÏ
select * from
people
where peopleId in (select   peopleId from   people group by   peopleId
having count
(peopleId) > 1)
2¡¢É¾³ý±íÖжàÓàµÄÖظ´¼Ç¼£¬Öظ´¼Ç¼ÊǸù¾Ýµ¥¸ö×ֶΣ¨p ......

ÔÚSQLÖÐSelectÓï¾äµÄÖ´ÐÐ˳Ðò

ÔÚSQLÖÐSelectÓï¾äµÄÖ´ÐÐ˳ÐòÈçÏ£º
(1)from×Ӿ䣬×é×°À´×Ô²»Í¬Êý¾ÝÔ´µÄÊý¾Ý
(2)WHERE×Ӿ䣬¸øÓèÖ¸¶¨Ìõ¼þ¶Ô¼Ç¼½øÐÐɸѡ
(3)GROUP BY×Ӿ䣬½«Êý¾Ý»®·ÖΪ¶à¸ö×é
(4)ʹÓþۼ¯º¯Êý½øÐмÆËã
(5)ʹÓÃHAVING×Ó¾äɸѡ·Ö×é
(6)¼ÆËãËùÓбí´ïʽ
(7)ʹÓÃORDER BY¶Ô½á¹û½øÐÐÅÅÐò ......

¶¯Ì¬sqlÓï¾ä»ù±¾Óï·¨

1 :ÆÕͨSQLÓï¾ä¿ÉÒÔÓÃExecÖ´ÐÐ
      Àý:      Select * from tableName
                Exec('select * from tableName')
        & ......

ÈçºÎÈÃÄãµÄSQLÔËÐеøü¿ì

 
 
ÈçºÎÈÃÄãµÄSQLÔËÐеøü¿ì
 
----   ÈËÃÇÔÚʹÓÃSQLʱÍùÍù»áÏÝÈëÒ»¸öÎóÇø£¬¼´Ì«¹Ø×¢ÓÚËùµÃµÄ½á¹ûÊÇ·ñÕýÈ·£¬¶øºöÂÔ   
  Á˲»Í¬µÄʵÏÖ·½·¨Ö®¼ä¿ÉÄÜ´æÔÚµÄÐÔÄܲîÒ죬ÕâÖÖÐÔÄܲîÒìÔÚ´óÐ͵ĻòÊǸ´ÔÓµÄÊý¾Ý¿â   
  »·¾³ÖУ¨ÈçÁª»úÊ ......

SQLѧϰ±Ê¼Ç

 2009-11-18 12:51:46
 ÏÞ¶¨´¦Àí¼Ç¼µÄµ¥Î»£¬rowcount=100±íʽÿ´Î´¦Àí100ÌõÊý¾Ý¡£Êµ¼Ê´¦ÀíµÄ¼Ç¼ÊýÓÉrowcountºÍwhere×Ó¾ä¾ö¶¨¡£Èç¹û·ûºÏwhereµÄ¼Ç¼Êý´óÓÚrowcount,ÔòÓÐrowcount¾ö¶¨£¬Èç¹ûСÓÚrowcount£¬ÔòÓÉwhere¾ö¶¨¡£
create table tb(id int identity(1,1),num int)
insert into tb
values(1)
while @@ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ