ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

9-1 »{ÃÑDML«ü¥O(9-1-1 Select«ü¥O)

9-1 »{ÃÑDML«ü¥O(9-1-1 Select«ü¥O)

9-1   »{ÃÑDML«ü¥O
DML¡G¸ê®Æ³B²z»y¨¥¡A¬°SQL¤¤ªº¸ê®Æ³B²z«ü¥O¸s¡A¥]¬ASelect¡]¬d¸ß¡^¡BInsert¡]·s¼W¡^¡BDelete¡]§R°£¡^¡BUpdate¡]­×§ï¡^µ¥«ü¥O¡C

9-1-1   Select«ü¥O
Select«ü¥O±z¤£­¯¥Í§a¡I¦b¤W¤@³¹¤¤¡Aµ§ªÌ´N°µ¤F«Ü¦hªº¤¶²Ð¡A²{¦bµ§ªÌ­n¤¶²Ðªº¬OSelect«ü¥Oªº¶i¶¥À³¥Î¡G

¸ó¸ê®Æªí¬d¸ß
µ§ªÌ²{¦bÁ|¤@­Ó¹ê¨Ò¨Ó»¡©ú¡G¦bLEAVE¸ê®Æ®w¤¤§t¦³¡ufactoryman¡]­û¤u¦W³æ¡^¡v¡B¡ulaik¡]¥ð°²°O¿ý¡^¡v³o¨â­Ó¸ê®Æªí¡G

¹Ï9-1  ½d¨Ò¸ê®Æ®w¤ºªº¸ê®Æªí¡C

²{¦b§Ú­n±q¡ufactoryman¡]­û¤u¦W³æ¡^¡v¸ê®Æªí¤¤¬D¥X¡ulaik¡]¥ð°²°O¿ý¡^¡v¸ê®Æªí¤º¥ð°²¡u¤Ñ¼Æ¡v¤j©ó¤T¤Ñªº­û¤u¸ê®Æ¡A¦pªG³æ¯Âªº¨Ï¥Î¤@­ÓSelect«ü¥O¬OµLªk¹F¨ì³o­Ó­n¨Dªº¡A¦]¦¹§Ú­Ì¥²¶·¨Ï¥Î¨â­ÓSelect«ü¥O¡I­º¥ý§Ú­Ì¥ý§Q¥Î¤U¦C±Ô­z§ä¥X¡ulaik¡]¥ð°²°O¿ý¡^¡v¸ê®Æªí¤º¥ð°²¡u¤Ñ¼Æ¡v¤j©ó¤T¤Ñªº­û¤u¸ê®Æ¡G
  1. Select ©m¦W  From  laik Where  ¤Ñ¼Æ>3
½Æ»s¥N½X
³o¼Ëªº¬d¸ßµ²ªG´N¦¨¬°¤@­Ó¡uµêÀÀ¡v¸ê®Æªí¡]¶°¡^¡A±µµÛ§Ú­Ì­n§Q¥Î³o­Ó¡uµêÀÀ¡v¸ê®Æªí¡]¶°¡^ªº¸ê®Æ¬°¶i¶¥¿z¿ï±ø¥ó­È¡A¿z¿ï¥X¡ufactoryman¡]­û¤u¦W³æ¡^¡v¸ê®Æªí¤¤³QÂI¦W¥ð°²¶W¹L3¤Ñªº­û¤u¸ê®Æ¡G
  1. Select  *  From  factoryman where ©m¦W In
  2. (Select ©m¦W From laik Where ¤Ñ¼Æ>3)
½Æ»s¥N½X
¦p¦¹¡A´N¥i¥H±q¡u­û¤u¦W³æ¡v¸ê®Æªí¤¤¬D¥X¡u¥ð°²°O¿ý¡v¸ê®Æªí¤º¥ð°²¡u¤Ñ¼Æ¡v¤j©ó¤T¤Ñªº­û¤u¸ê®Æ¡I

¹Ï9-2   SubQuery¡C
¤W¹Ï¤¤¡G¥¼¥[®Ø³¡¤ÀªºSelect«ü¥O±Ô­z¤¤§t¦³¥t¤@¬q³Q®Ø½u®Ø°_¨ÓªºSelect«ü¥O±Ô­z¡A³oºØÂùSelect«ü¥O²Õ¦Xªº±Ô­zºÙ¤§¬°¡uSubQuery¡v¡I
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

·í§Ú­Ì±N³o¡u¶i¶¥¬d¸ß«ü¥O¡vÀ³¥Î©óµ{¦¡¤¤±N¥i±o¨ì¹Ï9-3ªºµ²ªG¡A³Q±ø¦C¥X¨Óªº­û¤u³£¬O¥ð°²¤Ñ¼Æ¶W¹L¤T¤ÑªÌ¡C
ex09_01.php
  1. <?
  2.     $conn = mysql_connect("localhost", "root", "12345a");
  3.     if(@mysql_select_db("LEAVE", $conn))
  4.      {
  5.       //«ü©w´£¨ú¸ê®Æªº®Õ¹ï¦r¤¸ªí
  6.       mysql_query("set character set big5");
  7.       //¸ê®Æ®w¦s¦b,«Ø¥ßSQL©R¥O¦r¦ê
  8.       $SQL="Select  *  From  Factoryman where ©m¦W In" .
  9.             "(Select ©m¦W From laik Where ¤Ñ¼Æ>3)";      //±N¦^¶Çµ²ªG¦s©ñ©óÅܼƤ¤
  10.       $datalist=mysql_query($SQL);
  11.       //¨ú±oÄæ¦ì¼Æ¶q
  12.       $fieldnum=mysql_num_fields($datalist);
  13. ?>
  14. <html>
  15. <head>
  16. <meta http-equiv="Content-Type" content="text/html; charset=big5">
  17. <title>½d¨Òex09_01</title>
  18. </head>
  19. <center><table border=1 width=100%>
  20. <?
  21. /**************¦C¥XÄæ¦ì©ïÀY***************/
  22. echo "<tr>";     
  23.       for ($x=0 ;$x<$fieldnum;$x++)
  24.          {
  25.           echo "<td>" . mysql_field_name($datalist,$x) . "</td>";
  26.          }
  27. echo "</tr>";   
  28. /**************¦C¥XÄæ¦ì¸ê®Æ***************/
  29.       //±N¸ê®Æ¿ýÂà´«¬°Äæ¦ì°}¦C¶°¦X
  30.       while ($fielddatas=mysql_fetch_array($datalist))
  31.           {
  32.              //¿é¥XÄæ¦ì¸ê®Æ
  33.              echo "<tr>";
  34.              for ($x=0;$x<$fieldnum;$x++)
  35.                 {
  36.                  echo "<td>" . $fielddatas[$x] . "</td>";
  37.                 }
  38.              echo "</tr>";  
  39.           }
  40. ?>
  41. <table></body>
  42. </html>
  43. <?
  44.      }
  45.     else
  46.      {
  47.       echo "¸ê®Æ®w¤£¦s¦b";
  48.      }
  49. ?>
½Æ»s¥N½X

¹Ï9-3  ½d¨Òex09_01.php¡C
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

¤À²Õ²Î­p Group By
¦b¡ulaik¡v¸ê®Æªí¤º¡A§Ú­Ì¥i¥Hµo²{­û¤u¥ð°²ªº¦¸¼Æ¥i¯à¤£¥u¤@¦¸¡A¤]³\½Ð¤F¤@¦¸¯f°²¡A¹L¤F´X¤Ñ¤S½Ð¤F¨Æ°²¡A¦]¦¹¥ð°²ªº°O¿ý´N¤£¥u¤@¦¸¡G

¹Ï9-4   ¦h¦¸¥ð°²°O¿ý¡C

¦pªG§Ú­Ì§Æ±æ±oª¾­û¤uªºÁ`¥ð°²¤Ñ¼Æ¨º§Ú­Ì¥i¥H¨Ï¥Î¡uSUM¡v¨ç¼Æ¨Ó²Î­p¡G
  1. Select Sum(¤Ñ¼Æ) AS ­û¤u¥ð°²Á`¤Ñ¼Æ From laik
½Æ»s¥N½X
¥i¬O§Ú­Ì±o¨ìªº¬O¥þ³¡­û¤uªº¥ð°²¤Ñ¼Æ£«¡A¨º§Ú§Æ±æ¤À§O±oª¾¨C¤@¦ì­û¤uªºÁ`¥ð°²¤Ñ¼Æ¡A¨º¤S§ï¦p¦ó³B²z¡H³o­Ó®É­Ô§Ú­Ì´N¥i¥H§Q¥Î¡uGroup By¡v«ü¥O¨Ó¶i¦æ¤À²Õ²Î­p¡G
  1. Select ©m¦W,Sum(¤Ñ¼Æ) AS ¥ð°²Á`¤Ñ¼Æ From laik Group By ©m¦W
½Æ»s¥N½X
°õ¦æ¤W¦Cªº«ü¥O±Ô­z«á¡Aµ²ªG¦p¤U¹Ï¡]½d¨Òex09_02.php¡^¡G

¹Ï9-5   ¤À²Õ²Î­p¤§°õ¦æµe­±¡C
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

¤À²Õ²Î­p¨Ã¤£­­©w¨Ï¥Î©ó³æ¤@Äæ¦ì¡A¸ê®Æªí¤¤ªº¨C¤@­ÓÄæ¦ì³£¥i¦¨¬°¤À²Õªº¨Ì¾Ú¡A¦P®É¡A§Ú­Ì¤]¥i¥H¨Ï¥Î¦h­ÓÄæ¦ì¨Ó§@¬°¤À²Õªº¨Ì¾Ú¡A¨Ò¦p¤U¦Cªº¤À²Õ«ü¥O±Ô­z¡G

¹Ï9-6   ¦hÄæ¤À²Õ¡C


¹Ï9-7  ½d¨Òex09_03.php¡C
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

¤À²Õ¿z¿ï
Group By«ü¥O¤F¥i¥H¥Î¨Ó±Æ§Ç¥~¡A¤]¥i¥H¦P®É¥[¤W±Æ§Ç«ü¥O¨Ó¶i¦æ¸ê®Æ±Æ§Ç¡A¨Ò¦p¡G§Ú­Ì¥H¡u°²§O¡v¬°¤À²Õ¨Ì¾Ú¡A§Q¥ÎOrder By«ü¥O°w¹ï¥ð°²¤Ñ¼Æ¨Ó±Æ§Ç¡G
  1. Select °²§O,Sum(¤Ñ¼Æ) AS ¥ð°²Á`¤Ñ¼Æ From laik Group By°²§O Order By¥ð°²Á`¤Ñ¼Æ
½Æ»s¥N½X
°£¤F±Æ§Ç¤§¥~¡A§Ú­ÌÁÙ¥i¥[¤WWhere«ü¥O¥ý¶i¦æ¸ê®Æ¿z¿ï«á¦A¶i¦æ¤À²Õ»P±Æ§Ç¡G
  1. Select °²§O,Sum(¤Ñ¼Æ) AS ¥ð°²Á`¤Ñ¼Æ From laik Where
  2. ¦~=86 Group By°²§O Order By¥ð°²Á`¤Ñ¼Æ
½Æ»s¥N½X
¤W¨â¦¡ªº±Ô­z°õ¦æµ²ªG¦p¹Ï9-8¡A­n¯S§Oª`·N¨ì¤@ÂI¡G¨Ï¥ÎWhere«ü¥O¶i¦æ¿z¿ï¡A¥u¯à°÷¦b­ì©l¸ê®Æªº±¡ªp¤U¶i¦æ¿z¿ïªº°Ê§@¡A¤]´N¬O»¡¡A¿z¿ï¸ê®Æªº°Ê§@¥²¶·µo¥Í©ó¤À²Õ°Ê§@¤§«e¡A¤£¯à¦b¤À²Õ°Ê§@§¹¦¨¤§«á¤~¨Ï¥ÎWhere«ü¥O¶i¦æ¤À²Õµ²ªGªº¿z¿ï¡I

¹Ï9-8  ¹Ï¥ª¬°¤À²Õ»P±Æ§Ç¡F¹Ï¥k¬°¸ê®Æ¿z¿ï«á¦A¶i¦æ¤À²Õ»P±Æ§Ç¡C
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

¦b¤À²Õ°Ê§@§¹¦¨¤§«á¡A§Ú­ÌÁÙ­n¹ï¤À²Õµ²ªGªº¸ê®Æ¶i¦æ¿z¿ï®É¡A§Ú­Ì¸Ó¦p¦ó³B²z¡H¤Á°O¡G¤£¯à¨Ï¥ÎWhere«ü¥O¡I§_«h±z·|¨£¨ì¤U¦Cªº¿ù»~°T®§¡G

¹Ï9-9   Where¿z¿ï¤£¯à¦b¤À²Õ§¹¦¨¤§«á¡C

¦pªG¦b¤À²Õ°Ê§@§¹¦¨¤§«á¤~¶i¦æ¸ê®Æ¿z¿ïªº°Ê§@¥²¶·¨Ï¥Î¡uHaving¡v«ü¥O¡A¨Ò¦p¤U¨Ò¡G¡]½d¨Òex09_04.php¡^
  1.       //¸ê®Æ®w¦s¦b,«Ø¥ßSQL©R¥O¦r¦ê
  2.       $SQL="Select ¦~,©m¦W,°²§O,sum(¤Ñ¼Æ) AS ¦~¥ð°²¼Æ From laik " .
  3.      "Group By ¦~,©m¦W,°²§O HAVING ¦~=86";
  4.       //±N¦^¶Çµ²ªG¦s©ñ©óÅܼƤ¤
  5.       $datalist=mysql_query($SQL);
  6.       //¨ú±oÄæ¦ì¼Æ¶q
  7.       $fieldnum=mysql_num_fields($datalist);
½Æ»s¥N½X

¹Ï9-10  ¨Ï¥ÎHaving«ü¥O¿z¿ï¤À²Õµ²ªG¡C
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

¨º§Ú¥i¤£¥i¥H§ó§ï¿z¿ïªº±ø¥ó­È¡H¥i¥H¡A±z¤£¦ý¥i¥H¨Ï¥Î¡uµêÀÀ¡vÄæ¦ì¬°¿z¿ïªº®Ú¾Ú¡A¦p¤U¨Ò¡G
  1. Select ¦~,©m¦W,°²§O,sum(¤Ñ¼Æ) AS ¦~¥ð°²¼Æ  From ¥ð°²°O¿ý
  2. Group By ¦~,©m¦W,°²§O Having ¦~¥ð°²¼Æ>2  
½Æ»s¥N½X
¤W¦¡¤¤ªº¡u¦~¥ð°²¼Æ¡vÁöµM¬O¡uSum(¤Ñ¼Æ)¡vªº­pºâµ²ªG¡A¥¦¤]¬O¤@­Ó¡uµêÀÀ¡vÄæ¦ì¡A¤£¬O¸ê®Æ®w¤¤ªº¯u¹êÄæ¦ì¡A¦ý¬O«o¯à¥Î¨Ó°µ¬°¿z¿ïªº®Ú¾Ú¡C
¦pªG±z­n¨Ï¥Î¡u­pºâ¡vÄæ¦ì°µ¬°¿z¿ï®Ú¾Ú¡A¤]¥i¥H¼¶¼g¦¨¤U¦¡¡G¡]½d¨Òex09_05.php¡^
  1. SQL="Select ¦~,©m¦W,°²§O,sum(¤Ñ¼Æ) AS ¦~¥ð°²¼Æ From ¥ð°²°O¿ý " &_
  2.      "Group By ¦~,©m¦W,°²§O Having sum(¤Ñ¼Æ)>2"
  3. CMD.CommandText=SQL
½Æ»s¥N½X
°õ¦æµ²ªG¦p¤U¹Ï¡G

¹Ï9-11  ½d¨Òex09_05.php¡C
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

        ÀR«ä¦Û¦b : ¤Ñ¤W³Ì¬ü¬O¬P¬P¡A¤H¥Í³Ì¬ü¬O·Å±¡¡C
ªð¦^¦Cªí ¤W¤@¥DÃD