Êý¾Ý¿âʵÑéËÄ£ºSQLÊý¾Ý²éѯ
Ò»¡¢ÊµÑéÄ¿µÄÓëÒªÇó£º
1£®ÊµÑéÄ¿µÄ
ÊìÁ·ÕÆÎÕSQL SELECTÓï¾ä£¬ÄÜÔËÓòéѯÓï¾äÍê³É¸÷ÖÖ²éѯ¡£ ʵÑéÄÚÈÝ £º
ÔÚʵÑéÈýºÍʵÑéËĵĻù´¡ÉÏÍê³É²éѯ²Ù×÷£¬½«ÌâÄ¿ÖеÄx, yµÈ¾ßÌ廯£º 1¡¢ Ò»°ã¼òµ¥²éѯ
£¨1£© ²»´øÌõ¼þµÄ²éѯָ¶¨×ֶΣ¨¿¼ÂÇÈ¥µôºÍ²»È¥µôÖظ´ÖµÁ½ÖÖÇé¿ö£©¡£ £¨2£© ²éѯij¸ö±íÖеÄËùÓмǼ¡£ £¨3£© ʹÓõ¥¸öÌõ¼þµÄ¼òµ¥²éѯ¡£
£¨4£© ʹÓöà¸öÌõ¼þ£¨AND¹Øϵ£©µÄ²éѯ¡£ £¨5£© ʹÓöà¸öÌõ¼þ£¨OR¹Øϵ£©µÄ²éѯ¡£
£¨6£© ʹÓöà¸öÌõ¼þ£¨»ìºÏANDºÍOR¹Øϵ£©µÄ²éѯ¡£ £¨7£© ʹÓôøNOTÔËËãµÄ²éѯ¡£ £¨8£© ʹÓÃBETWEEN¡¤¡¤¡¤AND¡¤¡¤¡¤µÄ²éѯ¡£ £¨9£© ʹÓÃNOT¡¤¡¤¡¤BETWEEN¡¤¡¤¡¤AND¡¤¡¤¡¤µÄ²éѯ¡£ £¨10£© ʹÓÃLIKEÔËËã·ûµÄ×Ö·û´®Æ¥Åä²éѯ¡£ £¨11£© ʹÓÃLIKEÔËËã·ûµÄÄ£°åÆ¥Åä²éѯ¡£ £¨12£© ²éѯ¿ÕÖµºÍ·Ç¿ÕÖµµÄ²éѯ £¨13£© ½á¹ûÒªÇóÅÅÐòµÄ²éѯ
£¨14£© ²éѯ½á¹û°´¶àÁÐÅÅÐò£¬²¢·Ö±ðÒªÇóÉýÐòºÍ½µÐòµÄ²éѯ¡£ £¨15£© ʹÓÃTOPÏÔʾǰÈô¸É¼Ç¼µÄ²éѯ¡£
£¨16£© ʹÓÃTOPÏÔʾǰÈô¸É¼Ç¼µÄ²éѯ£¬Èç¹ûÓÐÂú×ãÌõ¼þµÄ²¢ÁмǼһ²¢
ÏÔʾ¡£
2¡¢Á¬½Ó²éѯ
£¨17£© Á½¸ö¹ØϵµÄÁ¬½Ó²éѯ¡£
£¨18£© ´øÆäËû²éѯÌõ¼þµÄÁ½¸ö¹ØϵµÄÁ¬½Ó²éѯ¡£ £¨19£© ¶à¸ö¹Øϵ£¨Èý¸öÒÔÉÏ£©µÄÁ¬½Ó²éѯ¡£ £¨20£© Á½¸ö¹ØϵµÄ¹ãÒåµÑ¿¨¶û»ýÔËËã½á¹û¡£
£¨21£© ¸ù¾ÝÁ½¸ö¹ØϵµÄ¹ãÒåµÑ¿¨¶û»ýÔËËã½á¹ûµÃµ½Á½¸ö¹Øϵ½øÐÐ×ÔÈ»Á¬½Ó
µÄ½á¹û¡£
£¨22£© ²éѯ½Ìʦ-¿Î³ÌÐÅÏ¢£¬²éѯ½á¹ûÖаüÀ¨½ÌʦÐÕÃû¡¢Ö°³Æ¡¢¿Î³ÌÃû³ÆºÍ
¿Î³ÌÐÔÖʵÈ4¸ö×ֶΡ£
£¨23£© ²éѯ½Ìʦ-¿Î³ÌÐÅÏ¢£¬²éѯ½á¹ûÖаüÀ¨½ÌʦÐÕÃû¡¢Ö°³Æ¡¢¿Î³ÌÃû³ÆºÍ
¿Î³ÌÐÔÖʵÈ4¸ö×ֶΣ¬ÒªÇó½á¹ûÖÐÁгöËùÓнÌʦÐÅÏ¢£¨¼´°üÀ¨²»ÊÇÈκογÌÔðÈνÌʦµÄ½ÌʦÐÅÏ¢£©¡£
£¨24£© ²éѯ½Ìʦ-¿Î³ÌÐÅÏ¢£¬²éѯ½á¹ûÖаüÀ¨½ÌʦÐÕÃû¡¢Ö°³Æ¡¢¿Î³ÌÃû³ÆºÍ
¿Î³ÌÐÔÖʵÈ4¸ö×ֶΣ¬ÒªÇó½á¹ûÖÐÄܹ»·´Ó³Ä¿Ç°Ã»ÓÐÈ·¶¨ÔðÈνÌʦµÄ¿Î³ÌÐÅÏ¢¡£
£¨25£© ²éѯ½Ìʦ-¿Î³ÌÐÅÏ¢£¬²éѯ½á¹ûÖаüÀ¨½ÌʦÐÕÃû¡¢Ö°³Æ¡¢¿Î³ÌÃû³ÆºÍ
¿Î³ÌÐÔÖʵÈ4¸ö×ֶΣ¬ÒªÇó½á¹ûÖмÈÄܹ»·´Ó³Ä¿Ç°²»ÊÇÔðÈνÌʦµÄ½ÌʦÐÅÏ¢£¬ÓÖÄÜ·´Ó³Ä¿Ç°Ã»ÓÐÈ·¶¨ÔðÈνÌʦµÄ¿Î³ÌÐÅÏ¢¡£ Ìáʾ£º£¨22£©~£¨25£©·Ö±ðÓÃÄÚÁ¬½Ó¡¢×óÁ¬½Ó¡¢ÓÒÁ¬½ÓÍê³É£¬·ÖÎöËüÃǵÄЧ¹û¡£ 3¡¢Ç¶Ìײéѯ£¨×¢ÒâʹÓÃANY¡¢ALLºÍEXISTS£© £¨26£© ʹÓÃINÔËËãµÄ¼òµ¥Ç¶Ìײéѯ¡£ £¨27£© ʹÓÃNOT INÔËËãµÄ¼òµ¥Ç¶Ìײéѯ¡£
£¨28£© ʹÓùØϵÔËË㣨ÈçµÈÓÚ£©µÄ¼òµ¥Ç¶Ìײéѯ¡£ £¨29£© ʹÓÃANY»òSOMEµÄ¼òµ¥Ç¶Ìײéѯ¡£ £¨30£© ʹÓÃALLµÄ¼òµ¥Ç¶Ìײéѯ¡£ £¨31£© ²éѯԺϵÃû³Æº¬¡°¼ÆËã»ú¡±¡¢Ö°³ÆΪ½ÌÊÚ¡¢Ëù¸ºÔð½Ì³ÌΪ±ØÐ޿εĽÌ
ʦÐÕÃû¡¢Ö°³Æ¡¢¿Î³ÌÃû³ÆºÍ¿Î³ÌѧʱµÈÐÅÏ¢£¨·Ö±ðÓÃǶÌײéѯºÍÁ¬½Ó²éѯÍê³É£¬·ÖÎö¸÷×ÔµÄЧÂÊ£©¡£
£¨32£© Éè¼ÆÁ½¸öÄÚÍâ²ã»¥Ïà¹ØµÄǶÌײéѯ¡£ £¨33£© ʹÓÃEXISTSµÄǶÌײéѯ¡£ £¨34£© ʹÓÃNOT EXISTSµÄǶÌײéѯ¡£ 4¡¢»ã×ܺͷÖ×é²éѯ
£¨35£© ʹÓÃCOUNTͳ¼ÆÊýÄ¿µÄ²éѯ¡£ £¨36£© ʹÓÃSUM¼ÆËãºÏ¼ÆµÄ²éѯ¡£
£¨37£© Ò»´ÎÍê³ÉÇóºÍ¡¢¼ÆÊý¡¢¼ÆËãƽ¾ùÖµµÄ²éѯ¡£
£¨38£© ²éѯËùÓпγ̵ijɼ¨¶¼´óÓÚ60·ÖµÄѧÉúµÄƽ¾ù·Ö×î¸ßµÄѧÉúÐÅÏ¢¡£ £¨39£© ²éѯÊý¾Ý¿â¿Î³ÌµÄ³É¼¨´óÓÚ70·ÖµÄ¡¢ËùÓпγÌƽ¾ù·Ö×î¸ßµÄѧÉúÐÅ
Ï¢¡£
£¨40£© ²éѯÿ¸öѧÉúµÄƽ¾ù³É¼¨¡£
£¨41£© ²éѯÿ¸öѧÉúµÄËùÓгɼ¨µÄ×î¸ß³É¼¨¡¢×îµÍ³É¼¨¡¢Æ½¾ù³É¼¨ºÍËù¿¼
¿Î³ÌµÄÃÅÊý¡£
£¨42£© ²éѯÖÁÉÙÓÐ10ÃűØÐ޿γ̿¼ÊԳɼ¨µÄÿ¸öѧÉúµÄƽ¾ù³É¼¨¡£ £¨43£© Éè¼Æ2¸öʹÓÃCOMPUTE¡¤¡¤¡¤BYºÍCOMPUTEµÄ²éѯ¡£ £¨44£© Éè¼Æ1¸öʹÓÃCOMPUTEµÄ²éѯ¡£ £¨45£© Éè¼Æ1¸öʹÓÃCOMPUTE¡¤¡¤¡¤BYµÄ²éѯ¡£
¶þ¡¢ÊµÑéÄÚÈÝ
1¡¢ÊµÑéÔÀí
SQL SELECT²éѯÓï¾äµÄÒ»°ã¸ñʽÊÇ£º
SELECT [ALL | DISTINCT] < ±í´ïʽ>[, <±í´ïʽ>¡] FROM <±íÃû>[,<±íÃû>¡] [WHERE <Âß¼±í´ïʽ>]
[GROUP BY <ÁÐÃû>[, <ÁÐÃû>¡][HAVING<ν´Ê>]]
[ORDER BY <ÁÐÃû> [ASC | DESC], [ <ÁÐÃû> [ASC | DESC]] ¡]
2¡¢ÊµÑé²½ÖèÓë½á¹û
£¨1£©µ÷³öSQL Server2005Èí¼þµÄÓû§½çÃ棬½øÈëSQL Server Management Studio¡£
£¨2£©ÊäÈë×Ô¼º±àºÃµÄ³ÌÐò¡£ £¨3£©¼ì²éÒÑÊäÈëµÄ³ÌÐòÕýÈ·Óë·ñ¡£
£¨4£©ÔËÐгÌÐò£¬²¢·ÖÎöÔËÐнá¹ûÊÇ·ñºÏÀíºÍÕýÈ·¡£ÔÚÔËÐÐʱҪעÒâµ±ÊäÈ벻ͬµÄÊý¾ÝʱËùµÃµ½µÄ½á¹ûÊÇ·ñÕýÈ·¡£
£¨5£©Êä³ö³ÌÐòÇåµ¥ºÍÔËÐнá¹û¡£ ʵÑé¹ý³Ì¼°Ïà¹Ø³ÌÐò£º 1¡¢Ò»°ã¼òµ¥²éѯ
£¨1£©²»´øÌõ¼þµÄ²éѯָ¶¨×ֶΣ¨¿¼ÂÇÈ¥µôºÍ²»È¥µôÖظ´ÖµÁ½ÖÖÇé¿ö£©¡£ select distinct ѧºÅ from Ñ¡¿Î
select ѧºÅ from Ñ¡¿Î
(2)²éѯij¸ö±íÖеÄËùÓмǼ ²éѯ½Ìʦ±íÖеÄËùÓмǼ
(3)ʹÓõ¥¸öÌõ¼þµÄ¼òµ¥²éѯ ²éѯ³É¼¨´óÓÚ50µÄѧÉúµÄѧºÅ select ѧºÅ from Ñ¡¿Î where ³É¼¨>50
£¨4£©Ê¹Óöà¸öÌõ¼þ£¨AND¹Øϵ£©µÄ²éѯ select ¹¤×Ê from ½Ìʦ
where ¹¤×Ê>200 and ¹¤×Ê<500
£¨5£©Ê¹Óöà¸öÌõ¼þ£¨OR¹Øϵ£©µÄ²éѯ
select ¹¤×Ê from ½Ìʦ
where ¹¤×Ê<200 or ¹¤×Ê>500
£¨6£©Ê¹Óöà¸öÌõ¼þ£¨»ìºÏANDºÍOR¹Øϵ£©µÄ²éѯ select * from ѧÉú
where (Ժϵ='2' and ÉúÔ´='»´ÄÏ') or (Ժϵ='6' and ÉúÔ´='»´±±')