¼òµ¥SQLÓï¾äС½á
ΪÁË´ó¼Ò¸üÈÝÒ×Àí½âÎÒ¾Ù³öµÄSQLÓï¾ä£¬±¾Îļٶ¨ÒѾ½¨Á¢ÁËÒ»¸öѧÉú³É¼¨¹ÜÀíÊý¾Ý¿â£¬È«ÎľùÒÔѧÉú³É¼¨µÄ¹ÜÀíΪÀýÀ´ÃèÊö¡£
¡¡¡¡1.ÔÚ²éѯ½á¹ûÖÐÏÔʾÁÐÃû£º
¡¡¡¡a.ÓÃas¹Ø¼ü×Ö£ºselect name as 'ÐÕÃû' from students order by age
¡¡¡¡b.Ö±½Ó±íʾ£ºselect name 'ÐÕÃû' from students order by age
¡¡¡¡2.¾«È·²éÕÒ:
¡¡¡¡a.ÓÃinÏÞ¶¨·¶Î§£ºselect * from students where native in ('ºþÄÏ', 'ËÄ´¨')
¡¡¡¡b.between...and£ºselect * from students where age between 20 and 30
¡¡¡¡c.“=”£ºselect * from students where name = 'Àîɽ'
¡¡¡¡d.like:select * from students where name like 'Àî%' (×¢Òâ²éѯÌõ¼þÖÐÓГ%”£¬Ôò˵Ã÷ÊDz¿·ÖÆ¥Å䣬¶øÇÒ»¹ÓÐÏȺóÐÅÏ¢ÔÚÀïÃ棬¼´²éÕÒÒÔ“ÀªÍ·µÄÆ¥ÅäÏî¡£ËùÒÔÈô²éѯÓГÀÄËùÓжÔÏó£¬Ó¦¸ÃÃüÁ'%Àî%';ÈôÊǵڶþ¸ö×ÖΪÀÔòӦΪ'_Àî%'»ò'_Àî'»ò'_Àî_'¡£)
¡¡¡¡e.[]Æ¥Åä¼ì²é·û£ºselect * from courses where cno like '[AC]%' (±íʾ»òµÄ¹Øϵ£¬Óë"in(...)"ÀàËÆ£¬¶øÇÒ"[]"¿ÉÒÔ±íʾ·¶Î§£¬È磺select * from courses where cno like '[A-C]%')
¡¡¡¡3.¶ÔÓÚʱ¼äÀàÐͱäÁ¿µÄ´¦Àí
¡¡¡¡a.smalldatetime£ºÖ±½Ó°´ÕÕ×Ö·û´®´¦ÀíµÄ·½Ê½½øÐд¦Àí£¬ÀýÈ磺
select * from students where birth > = '1980-1-1' and birth <= '1980-12-31'
¡¡¡¡4.¼¯º¯Êý
¡¡¡¡a.count()ÇóºÍ£¬È磺select count(*) from students (ÇóѧÉú×ÜÈËÊý)
¡¡¡¡b.avg(ÁÐ)Çóƽ¾ù£¬È磺select avg(mark) from grades where cno=’B2’
¡¡¡¡c.max(ÁÐ)ºÍmin(ÁÐ)£¬Çó×î´óÓë×îС
¡¡¡¡5.·Ö×égroup
¡¡¡¡³£ÓÃÓÚͳ¼Æʱ£¬Èç·Ö×é²é×ÜÊý£º
select gender,count(sno)
from students
group by gender
(²é¿´ÄÐŮѧÉú¸÷ÓжàÉÙ)
¡¡¡¡×¢Ò⣺´ÓÄÄÖֽǶȷÖ×é¾Í´ÓÄÄÁÐ"group by"
¡¡¡¡¶ÔÓÚ¶àÖØ·Ö×飬ֻÐ轫·Ö×é¹æÔòÂÞÁС£±ÈÈç²éѯ¸÷½ì¸÷רҵµÄÄÐŮͬѧÈËÊý £¬ÄÇô·Ö×é¹æÔòÓУº½ì±ð(grade)¡¢×¨Òµ(mno)ºÍÐÔ±ð(gender)£¬ËùÒÔÓÐ"group by grade, mno, gender"
select grade, mno, gender, count(*)
from students
group by grade, mno, gender
¡¡¡¡Í¨³£group»¹ºÍhavingÁªÓ㬱ÈÈç²éѯ1ÃÅ¿ÎÒÔÉϲ»¼°¸ñµÄѧÉú£¬Ôò°´Ñ§ºÅ(sno)·ÖÀàÓУº
select sno,count(*) from grades
where mark<60
group by sno
having count(*)>1
¡¡¡¡6.UNIONÁªºÏ
¡¡¡¡ºÏ²¢²éѯ½á¹û£¬È磺
SELECT * from students
W
Ïà¹ØÎĵµ£º
ϵͳ»·¾³£ºWindows 7
Èí¼þ»·¾³£ºVisual C++ 2008 SP1 +SQL Server 2005
±¾´ÎÄ¿µÄ£º±àдһ¸öº½¿Õ¹ÜÀíϵͳ
ÕâÊÇÊý¾Ý¿â¿Î³ÌÉè¼ÆµÄ³É¹û£¬ËäÈ»³É¼¨²»¼Ñ£¬µ«ÊÇ×÷ΪÎÒÓÃVC++ ÒÔÀ´±àдµÄ×î´ó³ÌÐò»¹ÊÇ´«µ½ÍøÉÏ£¬ÒÔ¹©²Î¿¼¡£ÓÃVC++ ×öÊý¾Ý¿âÉè¼Æ²¢²»ÈÝÒ×£¬µ«Ò²²»ÊDz»¿ÉÄÜ¡£ÒÔÏÂÊÇÎҵijÌÐò½çÃ棬ºóÃæ ......
ͨ³££¬ÄãÐèÒª»ñµÃµ±Ç°ÈÕÆںͼÆËãһЩÆäËûµÄÈÕÆÚ£¬ÀýÈ磬ÄãµÄ³ÌÐò¿ÉÄÜÐèÒªÅжÏÒ»¸öÔµĵÚÒ»Ìì»òÕß×îºóÒ»Ìì¡£ÄãÃǴ󲿷ÖÈË´ó¸Å¶¼ÖªµÀÔõÑù°ÑÈÕÆÚ½øÐзָÄê¡¢Ô¡¢Èյȣ©£¬È»ºó½ö½öÓ÷ָî³öÀ´µÄÄê¡¢Ô¡¢ÈյȷÅÔÚ¼¸¸öº¯ÊýÖмÆËã³ö×Ô¼ºËùÐèÒªµÄÈÕÆÚ£¡
:B;wrN$EEx)[ aL0
ÔÚÕâƪÎÄÕÂÀÎÒ½«¸æËßÄãÈçºÎʹÓÃDATEADDºÍ ......
һֱûÓÐÕÒµ½Ò»Öֺõķ½·¨À´ÒýÓÃÓзµ»ØÖµµÄ´æ´¢¹ý³ÌµÄ·½·¨£¬Ê¹ÎÒÔÚÌí¼ÓÊý¾ÝÖÐ×ßÁ˲»ÉÙµÄÍä·£¬×î½ü£¬ÔÚ²éÔÄÁË´óÁ¿µÄ×ÊÁÏÖ®ºó£¬ÖÕÓÚÔÚ΢ÈíµÄÒ»¸öʵÀýÖÐÕÒµ½ÁËÒ»ÖÖÁ¼ºÃµÄ·½·¨¡£
Ê×ÏȱàдºÃÒ»Óзµ»ØÖµµÄ´æ´¢¹ý³Ì
create procedure proc_name
@para1 nchar(20), --ÊäÈë²ÎÊý
&n ......
1.´ò¿ªSQL Server Management Studio ExpressÒÔ“WindowsÉí·ÝÈÏÖ¤”µÄ·½Ê½µÇ¼¡£Èçͼ1£º
ͼ1£º
2.ÔÚSQL Server Management Studio ExpressÖеē¶ÔÏó×ÊÔ´¹ÜÀíÆ÷”--“Êý¾Ý¿â”ÉÏÓÒ¼ü--“¸½¼Ó”¡£Èçͼ2£º
ͼ2£º
3.ÔÚµ¯³öÀ´µÄ“¸½¼ÓÊý¾Ý¿â”´°¿ÚÖе¥» ......
table
Num createDate
---------------------------------
(¿Õ) 20090901
(¿Õ) 20090901
  ......