18-3 �ϥ� SQL ���˵����

§Ú­Ì¦b¤W¤@¸`¤¶²Ð¤F¦p¦ó¨Ï¥Î ASP ¨ÓŪ¨ú¸ê®Æ®w¤¤ªº¸ê®Æ¡A¨Ã±N¨ä§e²{©óºô­¶¤¤¡CASP »P¸ê®Æ®w·¾³qªº¼Ð·Ç»y¨¥´N¬O SQL¡A¸g¥Ñ¦¹ºØ»y¨¥¡A§Ú­Ì¤~¯à¦b ASP µ{¦¡½X¹ï¸ê®Æ®w¶i¦æÀ˵ø¡B·s¼W¡B­×§ï¡B§R°£µ¥°Ê§@¡C¥»¸`±N¹ï SQL ¶i¤@¨B¤¶²Ð¨ä¥Îªk¡C

SQL ¬O¡uµ²ºc¤Æ¬d¸ß»y¨¥¡v¡]Structured Query Language¡^ªºÂ²ºÙ¡A¬O¥Ñ IBM ¤½¥q©ó 1970 ¦~¥N©Òµo®i¥X¨Ó¡A¥Î©óÃöÁp¦¡¸ê®Æ®w (Relational Databases) ·í¤¤ªº¤@ºØ¸ê®Æ®w¬d¸ß»y¨¥¡A§Q¥Î SQL ¥i¥H¥Î¨Ó¶i¦æ¦UºØ»P¸ê®Æ®w¬ÛÃöªº³B²z¡A¨Ò¦p¡G

¥Ñ©ó SQL ¥\¯à©w¸q§¹¾ã¡A©Ò¥H¤w¸g¦¨¬°¤@­Ó¬d¸ß¸ê®Æ®wªº¼Ð·Ç»y¨¥¡CÁöµM¦U®a¸ê®Æ®w©Ò´£¨Ñªº SQL »y¨¥¦b¥\¯à¤W·|²¤¦³®t²§¡A¦ý°ò¥»ªº¥\¯à¬O¤@­Pªº¡C¥»³æ¤¸±N ASP »P¸ê®Æ®w¾ã¦X³Ì±`¥Î¨ìªºSQL»yªk°µ¤@­Ó¾ã²z¡A¥Ñ©ó SQL ªº»yªk¬Û·í¥­©öªñ¤H¡A©Ò¥HŪªÌ¥u­n¹ï¥H¤Uªº»yªkµy°µ¤F¸Ñ¤§«á¡A§Y¥i°¨¤W¶i¤J ASP »P¸ê®Æ®w¾ã¦Xªº¥@¬É¡C

¥ô¦ó¸ê®Æ®w³£¦³¥|­Ó°ò¥»¬d¸ß°Ê§@¡A§YÀ˵ø¡B·s¼W¡B­×§ï¡B§R°£¡A¥H¤U±N³v¤@¤¶²Ð³o¥|ºØ°ò¥»¥\¯àªº SQL »yªk¡A¥H¤Î¬ÛÃöªº½d¨Ò¡C

­Y­nÀ˵ø¸ê®Æ®wªº¸ê®Æ¡A¨Ï¥Îªº SQL ¥D­n«ü¥O¬O¡uSELECT¡v¡A°ò¥»»yªk¦p¤U¡G

SELECT Äæ¦ì¦WºÙ1, Äæ¦ì¦WºÙ2, ... FROM ¸ê®Æªí¦WºÙ1, ¸ê®Æªí¦WºÙ2, ... [WHERE ±ø¥ó¦¡] [ORDER BY Äæ¦ì¦WºÙ1, Äæ¦ì¦WºÙ2, ...] »¡©ú¦p¤U¡G SQL ªº³]­p°ò¥»¤W¬O¼Ò¥é­^¤åªº¦ÛµM»yªk¡A¦]¦¹¦b¤Jªù¤W¸û¬°®e©ö¡C±µµÛ§Ú­Ì¨Ó¬Ý¬Ý´X­Ó½d¨Ò¡A´N¥i¥HÁA¸Ñ SQL ªººëÅè¡C

­º¥ý¡A§Ú­Ì¥H¸ê®Æ®w asr/example/database/basketball.mdb ¬°¨Ò¡A³o­Ó¸ê®Æ®w¥]§t¨â­Ó¸ê®Æªí¡G

¬ÛÃö¤º®e¦p¤U¡G
¸ê®Æ®w "example/database/basketball.mdb"
¸ê®Æªí "Player" ªº¤º®e ¸ê®Æªí "Team" ªº¤º®e
ID NickName Name TeamID Percentage
jean §d§Ó»Ê 38.25 
jones ±i¯¯¹Å 49.77 
ben ³¯§¶±l 50.26 
asser ªL´f®S 37.22 
window §õ©y´­ 36.67 
roger ±i´¼¬P 25.88 
cosh ³\¤å»¨ 70.98 
banny ¬xÄPµ¾ 88.97 
shyba ªô¤¤¤H 67.45 
10 batty ·¨Âz¦p 65.55 
11 joey ³\¹Å®Ê 47.65 
12 roland §d·ç¤d 55.87 
13 sony ªL¹|µØ 54.77 
14 beball ¸­¨Î¼z 33.33 
15 gavins ªL¬F·½ 55.65 
16 jojo ³¯«T³Ç 44.65 
17 jtchen ³¯¦¿§ø 48.76 
18 Gao °ª¦W´­ 67.88 
19 Wayne ³¯´¼°¶ 65.87 
20 chingz ³¯´¸ 57.28 
ID Name WinNo
¥x¥_¶¤ 12 
·s¦Ë¶¤ 
¥x¤¤¶¤ 10 
«n§ë¶¤ 12 
¥x«n¶¤ 17 
°ª¶¯¶¤ 16 
¼ê´ò¶¤ 11 

¥H¤U¬O´X­ÓÀ˵ø«ü¥Oªº°ò¥»½d¨Ò¡A³£¥u¦³²o¯A¨ì³æ¤@¸ê®Æªí¡C¡]ŪªÌ¥i¥H¥u¬Ý SQL «ü¥O¡A²q²q¥¦ªº·N¸q¡A¦A¬Ý¬Ý¤¤¤å¸ÑĶ¡C¡^

  1. SQL«ü¥O¡GSELECT * FROM Team
    ·N¸q¡G©Ò¦³²y¶¤¸ê®Æ
    »¡©ú¡G¡u*¡v¥Nªí Team ¸ê®Æªí¤¤©Ò¦³ªºÄæ¦ì¡C
    ¬d¸ßµ²ªG¡G
    ID Name WinNo
    ¥x¥_¶¤ 12 
    ·s¦Ë¶¤ 
    ¥x¤¤¶¤ 10 
    «n§ë¶¤ 12 
    ¥x«n¶¤ 17 
    °ª¶¯¶¤ 16 
    ¼ê´ò¶¤ 11 
  2. SQL«ü¥O¡GSELECT TOP 3 * FROM Team
    ·N¸q¡G©Ò¦³²y¶¤¸ê®Æ¡A¦ý¥u§ì«e¤Tµ§
    »¡©ú¡G¡uTOP 3¡v¥Nªí¥u§ì¨ú«e¤Tµ§¸ê®Æ¡C¤]¥i¥H¨Ï¥Î¡uTOP 25 percent¡vµ¥¡A¥Nªí§ì¨ú©Ò¦³¸ê®Æªº«e¦Ê¤À¤§¤G¤Q¤­¡C
    ¬d¸ßµ²ªG¡G
    ID Name WinNo
    ¥x¥_¶¤ 12 
    ·s¦Ë¶¤ 
    ¥x¤¤¶¤ 10 
  3. SQL«ü¥O¡GSELECT Name, Percentage FROM Player WHERE NickName='gavins'
    ·N¸q¡Gºï¸¹¬° gavins ªº²y­û©m¦W¤Î©R¤¤²v
    ¬d¸ßµ²ªG¡G
    Name Percentage
    ªL¬F·½ 55.65 
  4. SQL«ü¥O¡GSELECT * FROM Team WHERE Name like '¥x%'
    ·N¸q¡G¶¤¦W¥H¡u¥x¡v¶}ÀYªº²y¶¤¸ê®Æ
    »¡©ú¡G¡u%¡v¥Nªí¥ô·Nªø«×ªº¦r¦ê¡C
    ¬d¸ßµ²ªG¡G
    ID Name WinNo
    ¥x¥_¶¤ 12 
    ¥x¤¤¶¤ 10 
    ¥x«n¶¤ 17 
  5. SQL«ü¥O¡GSELECT Name, Percentage FROM Player WHERE Name like '³¯__'
    ·N¸q¡G¡u©m³¯¥B¦W¦r¦³¤T­Ó¦r¡vªº²y­û©m¦W¤Î©R¤¤²v
    »¡©ú¡G¡u_¡v¥Nªí¥ô·N³æ¤@¦r¤¸¡C
    ¬d¸ßµ²ªG¡G
    Name Percentage
    ³¯§¶±l 50.26 
    ³¯«T³Ç 44.65 
    ³¯¦¿§ø 48.76 
    ³¯´¼°¶ 65.87 
  6. SQL«ü¥O¡GSELECT Name, WinNo FROM Team WHERE WinNo>10
    ·N¸q¡G¡u³Ó³õ¼Æ¤j©ó10¡vªº²y¶¤¦WºÙ¤Î¨ä³Ó³õ¼Æ
    ¬d¸ßµ²ªG¡G
    Name WinNo
    ¥x¥_¶¤ 12 
    «n§ë¶¤ 12 
    ¥x«n¶¤ 17 
    °ª¶¯¶¤ 16 
    ¼ê´ò¶¤ 11 
  7. SQL«ü¥O¡GSELECT Name, WinNo FROM Team WHERE WinNo>10 ORDER BY WinNo DESC
    ·N¸q¡G¡u³Ó³õ¼Æ¤j©ó10¡vªº²y¶¤¦WºÙ¤Î¨ä³Ó³õ¼Æ¡A¨Ã®Ú¾Ú³Ó³õ¼Æ¥Ñ¤j¨ì¤p±Æ¦C
    »¡©ú¡G­Y¤£¥[¤J DESC¡A«h·|¶i¦æ¥Ñ¤p¨ì¤jªº±Æ§Ç¡C
    ¬d¸ßµ²ªG¡G
    Name WinNo
    ¥x«n¶¤ 17 
    °ª¶¯¶¤ 16 
    «n§ë¶¤ 12 
    ¥x¥_¶¤ 12 
    ¼ê´ò¶¤ 11 
  8. SQL«ü¥O¡GSELECT TeamID, Name, Percentage FROM Player WHERE TeamID=5 ORDER BY Percentage DESC
    ·N¸q¡G¡u²y¶¤¥N½X¬°5¡vªº²y­û©R¤¤²v±Æ¦æº]
    ¬d¸ßµ²ªG¡G
    TeamID Name Percentage
    ªô¤¤¤H 67.45 
    ³¯´¸ 57.28 
    ªL¬F·½ 55.65 
    ±i¯¯¹Å 49.77 
    ³¯«T³Ç 44.65 
    ¸­¨Î¼z 33.33 
  9. SQL«ü¥O¡GSELECT * FROM Player ORDER BY TeamID, Percentage DESC
    ·N¸q¡G¨C¤@¶¤ªº²y­û©R¤¤²v±Æ¦æº]
    »¡©ú¡G¦C¥Xµ²ªG·|¥ý«ö TeamID ¥Ñ¤p¨ì¤j±Æ§Ç¡A¦A«ö Percentage ¥Ñ¤j¨ì¤p±Æ§Ç¡C
    ¬d¸ßµ²ªG¡G
    ID NickName Name TeamID Percentage
    18 Gao °ª¦W´­ 67.88 
    12 roland §d·ç¤d 55.87 
    13 sony ªL¹|µØ 54.77 
    ben ³¯§¶±l 50.26 
    jean §d§Ó»Ê 38.25 
    window §õ©y´­ 36.67 
    roger ±i´¼¬P 25.88 
    17 jtchen ³¯¦¿§ø 48.76 
    11 joey ³\¹Å®Ê 47.65 
    asser ªL´f®S 37.22 
    10 batty ·¨Âz¦p 65.55 
    shyba ªô¤¤¤H 67.45 
    20 chingz ³¯´¸ 57.28 
    15 gavins ªL¬F·½ 55.65 
    jones ±i¯¯¹Å 49.77 
    16 jojo ³¯«T³Ç 44.65 
    14 beball ¸­¨Î¼z 33.33 
    banny ¬xÄPµ¾ 88.97 
    cosh ³\¤å»¨ 70.98 
    19 Wayne ³¯´¼°¶ 65.87 
  10. SQL«ü¥O¡GSELECT count(*) FROM Team WHERE WinNo>10
    ·N¸q¡G¡u³Ó³õ¼Æ¤j©ó10¡vªº²y¶¤Á`¼Æ
    »¡©ú¡Gcount()¨ç¼Æ·|­pºâ¸ê®Æµ§¼Æ¡A¸ê®Æ®w·|¦Û°Ê²£¥Í¼È®ÉªºÄæ¦ì¦WºÙ Expr1000¡C
    ¬d¸ßµ²ªG¡G
    Expr1000
  11. SQL«ü¥O¡GSELECT max(Percentage) as ³Ì°ª©R¤¤²v FROM Player
    ·N¸q¡G©Ò¦³²y­ûªº³Ì°ª©R¤¤²v
    »¡©ú¡Gmax(Percentage)¨ç¼Æ·|­pºâ©R¤¤²v³Ì¤j­È¡C¥Ñ©ó¨Ï¥Î¤F¡uas ³Ì°ª©R¤¤²v¡v¡A¸ê®Æ®w·|¦Û°Ê²£¥Í¼È®ÉªºÄæ¦ì¦WºÙ¡u³Ì°ª©R¤¤²v¡v¡C
    ¬d¸ßµ²ªG¡G
    ³Ì°ª©R¤¤²v
    88.97 
  12. SQL«ü¥O¡GSELECT TOP 1 Name, Percentage FROM Player ORDER BY Percentage DESC
    ·N¸q¡G¨ã¦³³Ì°ª©R¤¤²vªº²y­û¸ê®Æ
    ¬d¸ßµ²ªG¡G
    Name Percentage
    ¬xÄPµ¾ 88.97 
  13. SQL«ü¥O¡GSELECT Name, Percentage FROM Player WHERE Percentage in (SELECT max(Percentage) FROM Player)
    ·N¸q¡G¨ã¦³³Ì°ª©R¤¤²vªº²y­û¸ê®Æ
    »¡©ú¡G¥\¯à¦P«e¤@­Ó½d¨Ò¡A¦ý¬O§ï¥Î¨â­Ó SQL «ü¥O²Õ¦X¨Ó¹F¦¨¦P¼Ëªº®ÄªG¡C
    ¬d¸ßµ²ªG¡G
    Name Percentage
    ¬xÄPµ¾ 88.97 
¦b¤W­z½d¨Ò¤¤¡A§Ú­Ì¤w¸gª¾¹D¦p¦ó¨Ï¥Î count ©Î¬O max ¨ç¼Æ¨Ó¶i¦æ²Î­p¡A¦ý­Y­n®Ú¾ÚÄæ¦ì­È¤£¦P¨Ó¶i¦æ²Î­p¡A´N¥²¶·¥Î¨ì¸s²Õ«ü¥O¡uGROUP BY¡v¡A­YÁÙ­n«ü©w¬ÛÃö±ø¥ó¡A´N¥²¶·¥Î¨ì¸s²Õ±ø¥ó¡uHAVING¡v¡A¦]¦¹ SQL ©Ò¨Ï¥Îªº SELECT «ü¥O®æ¦¡´N·|¤ñ¸û½ÆÂø¡A¦p¤U¡G SELECT Äæ¦ì¦WºÙ1, Äæ¦ì¦WºÙ2, ... FROM ¸ê®Æªí¦WºÙ1, ¸ê®Æªí¦WºÙ2, ... [WHERE ±ø¥ó¦¡] [GROUP BY Äæ¦ì¦WºÙ1, Äæ¦ì¦WºÙ2, ...] [HAVING ±ø¥ó¦¡] [ORDER BY Äæ¦ì¦WºÙ1, Äæ¦ì¦WºÙ2, ...] §Ú­Ì°w¹ï·s¼Wªº±Ô­z¨Ó»¡©ú¦p¤U¡G ¥H¤U¬O´X­Ó¨Ï¥Î¨ì GROUP BY ¤Î HAVING ªº SQL ½d¨Ò¡G
  1. SQL«ü¥O¡GSELECT TeamID, count(*) as ²y­û¤H¼Æ, avg(Percentage) as ¥­§¡©R¤¤²v FROM Player GROUP BY TeamID
    ·N¸q¡G¨C­Ó²y¶¤ªº²y­û¤H¼Æ¤Î¥­§¡©R¤¤²v
    »¡©ú¡Gavg(Percentage) ¥i¥H­pºâ©R¤¤²v¥­§¡­È¡AÃþ¦üªº SQL »E¦X¨ç¼Æ¦³ Avg¡]¥­§¡­È¡^¡BCount¡]µ§¼Æ¡^¡BMax¡]³Ì¤j­È¡^¡BMin¡]³Ì¤p­È¡^¡BStDev¡]¥À¸sÅé¼Ë¥»¼Ð·Ç®t¡^¡BStDevp¡]¥À¸sÅé¼Ð·Ç®t¡^¡BSum¡]Á`©M¡^¡BVar¡]¥À¸sÅé¼Ë¥»Åܲ§¼Æ¡^¡BVarP¡]¥À¸sÅéÅܲ§¼Æ¡^µ¥¡C¥Ñ©ó³o¬O¹ï©ó¨C­Ó²y¶¤ªº²Î­p¼Æ¦r¡A©Ò¥H¥²¶·¥Î¨ì¸s²Õ«ü¥O¡uGROUP BY¡v¡C
    ¬d¸ßµ²ªG¡G
    TeamID ²y­û¤H¼Æ ¥­§¡©R¤¤²v
    50.61666666666667 
    25.88 
    44.54333333333333 
    65.55 
    51.355 
    79.975 
    65.87 
  2. SQL«ü¥O¡GSELECT TeamID, count(*) as ²y­û¤H¼Æ FROM Player GROUP BY TeamID HAVING count(*)>2
    ·N¸q¡G¨C­Ó²y¶¤ªº²y­û¤H¼Æ¡A¦ý¥uÅã¥Ü²y­û¤H¼Æ¤j©ó 2 ¦ìªº¸ê®Æ
    »¡©ú¡Gavg(Percentage) ¥i¥H­pºâ©R¤¤²v¥­§¡­È¡C¥Ñ©ó³o¬O¹ï©ó¨C­Ó²y¶¤ªº²Î­p¼Æ¦r¡A©Ò¥H¥²¶·¥Î¨ì¸s²Õ«ü¥O¡uGROUP BY¡v¡A¬ÛÃöªº±ø¥ó«h¥²¶·¨Ï¥Î¡uHAVING¡v¨Ó«ü©w¡C
    ¬d¸ßµ²ªG¡G
    TeamID ²y­û¤H¼Æ

¥H¤W½d¨Ò³£¬O¥u¦³°w¹ï¤@­Ó¸ê®Æªí¨Ó¶i¦æÀ˵ø¬d¸ß¡A¤U¦Cªº½d¨Ò«h¬O®Ú¾Ú³o¨â­Ó¸ê®ÆªíªºÃöÁp©Ê¨Ó¶i¦æÀ˵ø¬d¸ß¡C

  1. SQL«ü¥O¡GSELECT Team.Name, Player.Name, Percentage FROM Player, Team WHERE ((Team.Name='¥x¥_¶¤') and (Player.TeamID=Team.ID))
    ·N¸q¡G¥x¥_¶¤ªº²y­û¸ê®Æ
    »¡©ú¡G¥Ñ©ó¨â­Ó¸ê®Æªí³£¦³ Name Äæ¦ì¡A©Ò¥H§Ú­Ì¥²¶·¨Ï¥Î Team.Name ¤Î Player.Name ¨Ó°Ï¤À¤£¦P¸ê®ÆªíªºÄæ¦ì¡C¥t¥~¡A³o¨â­Ó¸ê®ÆªíªºÃöÁp©Ê¬O¥Ñ(Player.TeamID=Team.ID) ©Ò«Ø¥ß¡A©Ò¥H¦b«áÄòªº½d¨Ò¤¤¡A§Ú­Ì·|¤£Â_¨Ï¥Î³o­Ó¬d¸ß±ø¥ó¡C
    ¬d¸ßµ²ªG¡G
    Name Name Percentage
    ¥x¥_¶¤ ³¯§¶±l 50.26 
    ¥x¥_¶¤ °ª¦W´­ 67.88 
    ¥x¥_¶¤ §õ©y´­ 36.67 
    ¥x¥_¶¤ ªL¹|µØ 54.77 
    ¥x¥_¶¤ §d·ç¤d 55.87 
    ¥x¥_¶¤ §d§Ó»Ê 38.25 
  2. SQL«ü¥O¡GSELECT Team.Name, Player.Name, Percentage FROM Player, Team WHERE (Player.TeamID=Team.ID) and (Team.Name IN ('°ª¶¯¶¤', '¥x¤¤¶¤')) ORDER BY Team.Name, Percentage DESC
    ·N¸q¡G°ª¶¯¶¤©M¥x¤¤¶¤ªº®g¤â±Æ¦æº]
    ¬d¸ßµ²ªG¡G
    Name Name Percentage
    ¥x¤¤¶¤ ³¯¦¿§ø 48.76 
    ¥x¤¤¶¤ ³\¹Å®Ê 47.65 
    ¥x¤¤¶¤ ªL´f®S 37.22 
    °ª¶¯¶¤ ¬xÄPµ¾ 88.97 
    °ª¶¯¶¤ ³\¤å»¨ 70.98 
  3. SQL«ü¥O¡GSELECT Team.Name as ²y¶¤¦WºÙ, Team.WinNo as Ĺ³õ¦¸¼Æ, count(*) as ²y­û¤H¼Æ, max(Percentage) as ³Ì°ª©R¤¤²v, min(Percentage) as ³Ì§C©R¤¤²v, avg(Percentage) as ¥­§¡©R¤¤²v FROM Player, Team WHERE ((Player.TeamID=Team.ID)) GROUP BY Team.Name, Team.WinNo
    ·N¸q¡G¨C­Ó²y¶¤ªº¬ÛÃö²Î­p¼Æ¦r
    »¡©ú¡G¥Ñ©ó³o¬O¹ï©ó¨C­Ó²y¶¤ªº²Î­p¼Æ¦r¡A©Ò¥H¥²¶·¥Î¨ì¸s²Õ«ü¥O¡uGROUP BY¡v¡C¦P®É¥Ñ©ó³Q¿ï¨úªºÄæ¦ì¤¤¡ATeam.Name ©M Team.WinNo ³£¨S¦³¥Î¨ì¥ô¦ó»E¦X¨ç¼Æ¡A©Ò¥H¦b GROUP BY ¤§«á¤]¥²¶·­n¥[¤W³o¨â­ÓÄæ¦ì¡C
    ¬d¸ßµ²ªG¡G
    ²y¶¤¦WºÙ Ĺ³õ¦¸¼Æ ²y­û¤H¼Æ ³Ì°ª©R¤¤²v ³Ì§C©R¤¤²v ¥­§¡©R¤¤²v
    ¥x¤¤¶¤ 10 48.76 37.22 44.54333333333333 
    ¥x¥_¶¤ 12 67.88 36.67 50.61666666666667 
    ¥x«n¶¤ 17 67.45 33.33 51.355 
    «n§ë¶¤ 12 65.55 65.55 65.55 
    °ª¶¯¶¤ 16 88.97 70.98 79.975 
    ·s¦Ë¶¤ 25.88 25.88 25.88 
    ¼ê´ò¶¤ 11 65.87 65.87 65.87 

¥t¥~ÁÙ¦³¤@¨Ç¨Ï¥Î SQL ¨ÓÀ˵ø¸ê®Æªº½d¨Ò¡A¥i¨£ asp/example/database/selectQuery01.asp¡A½ÐŪªÌ¦Û¦æ¸Õ¬Ý¬Ý¡C

¤U¤@¤p¸`±N»¡©ú¦p¦ó¨Ï¥Î SQL «ü¥O¨Ó¹ï¸ê®Æ®w¶i¦æ·s¼W¡B­×§ï¡B§R°£¸ê®Æµ¥°Ê§@¡C


JScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©ó¦øªA¾¹ºÝªº ASP Àô¹Ò