ÓÅ»¯MySQLÊý¾Ý¿âÐÔÄܵļ¸¸öºÃ·½·¨_MySQLÊý¾Ý¿â_ºÚ¿Í·ÀÏßÍø°²·þÎñÆ÷ά»¤»ùµØ--Powered by WWW.RONGSEN.COM.CN

ÓÅ»¯MySQLÊý¾Ý¿âÊý¾Ý¿âÐÔÄܵļ¸¸öºÃ·½·¨

×÷ÕߣººÚ¿Í·ÀÏßÍø°²MYSQLά»¤»ùµØ À´Ô´£ººÚ¿Í·ÀÏßÍø°²MYSQLά»¤»ùµØ ä¯ÀÀ´ÎÊý£º0

±¾Æª¹Ø¼ü´Ê£º·½·¨ÐÔÄÜÓÅ»¯²éѯ
ºÚ¿Í·ÀÏßÍø°²ÍøѶ£º¡¡¡¡1¡¢Ñ¡È¡×îÊÊÓõÄ×Ö¶ÎÊôÐÔ ¡¡¡¡MySQL¿ÉÒԺܺõÄÖ§³Ö´óÊý¾ÝÁ¿µÄ´æÈ¡£¬µ«ÊÇÒ»°ã˵À´£¬Êý¾Ý¿âÖеıíԽС£¬ÔÚËüÉÏÃæÖ´ÐеIJéѯҲ¾Í»áÔ½¿ì¡£Òò´Ë£¬ÔÚ´´½¨±íµÄʱºò£¬ÎªÁË»ñµÃ¸üºÃµÄÐÔÄÜ£¬ÎÒÃÇ¿É...

¡¡¡¡1¡¢Ñ¡È¡×îÊÊÓõÄ×Ö¶ÎÊôÐÔ

¡¡¡¡MySQL¿ÉÒԺܺõÄÖ§³Ö´óÊý¾ÝÁ¿µÄ´æÈ¡£¬µ«ÊÇÒ»°ã˵À´£¬Êý¾Ý¿âÖеıíԽС£¬ÔÚËüÉÏÃæÖ´ÐеIJéѯҲ¾Í»áÔ½¿ì¡£Òò´Ë£¬ÔÚ´´½¨±íµÄʱºò£¬ÎªÁË»ñµÃ¸üºÃµÄÐÔÄÜ£¬ÎÒÃÇ¿ÉÒÔ½«±íÖÐ×ֶεĿí¶ÈÉèµÃ¾¡¿ÉÄÜС¡£ÀýÈ磬ÔÚ¶¨ÒåÓÊÕþ±àÂëÕâ¸ö×Ö¶Îʱ£¬Èç¹û½«ÆäÉèÖÃΪCHAR(255),ÏÔÈ»¸øÊý¾Ý¿âÔö¼ÓÁ˲»±ØÒªµÄ¿Õ¼ä£¬ÉõÖÁʹÓÃVARCHARÕâÖÖÀàÐÍÒ²ÊǶàÓàµÄ£¬ÒòΪCHAR(6)¾Í¿ÉÒԺܺõÄÍê³ÉÈÎÎñÁË¡£Í¬ÑùµÄ£¬Èç¹û¿ÉÒԵĻ°£¬ÎÒÃÇÓ¦¸ÃʹÓÃMEDIUMINT¶ø²»ÊÇBIGINÀ´¶¨ÒåÕûÐÍ×ֶΡ£

¡¡¡¡ÁíÍâÒ»¸öÌá¸ßЧÂʵķ½·¨ÊÇÔÚ¿ÉÄܵÄÇé¿öÏ£¬Ó¦¸Ã¾¡Á¿°Ñ×Ö¶ÎÉèÖÃΪNOT NULL£¬ÕâÑùÔÚ½«À´Ö´ÐвéѯµÄʱºò£¬Êý¾Ý¿â²»ÓÃÈ¥±È½ÏNULLÖµ¡£

¡¡¡¡¶ÔÓÚijЩÎı¾×ֶΣ¬ÀýÈç¡°Ê¡·Ý¡±»òÕß¡°ÐԱ𡱣¬ÎÒÃÇ¿ÉÒÔ½«ËüÃǶ¨ÒåΪENUMÀàÐÍ¡£ÒòΪÔÚMySQLÖУ¬ENUMÀàÐͱ»µ±×÷ÊýÖµÐÍÊý¾ÝÀ´´¦Àí£¬¶øÊýÖµÐÍÊý¾Ý±»´¦ÀíÆðÀ´µÄËÙ¶ÈÒª±ÈÎı¾ÀàÐÍ¿ìµÃ¶à¡£ÕâÑù£¬ÎÒÃÇÓÖ¿ÉÒÔÌá¸ßÊý¾Ý¿âµÄÐÔÄÜ¡£

¡¡¡¡2¡¢Ê¹ÓÃÁ¬½Ó£¨JOIN£©À´´úÌæ×Ó²éѯ(Sub-Queries)

¡¡¡¡MySQL´Ó4.1¿ªÊ¼Ö§³ÖSQLµÄ×Ó²éѯ¡£Õâ¸ö¼¼Êõ¿ÉÒÔʹÓÃSELECTÓï¾äÀ´´´½¨Ò»¸öµ¥ÁеIJéѯ½á¹û£¬È»ºó°ÑÕâ¸ö½á¹û×÷Ϊ¹ýÂËÌõ¼þÓÃÔÚÁíÒ»¸ö²éѯÖС£ÀýÈ磬ÎÒÃÇÒª½«¿Í»§»ù±¾ÐÅÏ¢±íÖÐûÓÐÈκζ©µ¥µÄ¿Í»§É¾³ýµô£¬¾Í¿ÉÒÔÀûÓÃ×Ó²éѯÏÈ´ÓÏúÊÛÐÅÏ¢±íÖн«ËùÓз¢³ö¶©µ¥µÄ¿Í»§IDÈ¡³öÀ´£¬È»ºó½«½á¹û´«µÝ¸øÖ÷²éѯ£¬ÈçÏÂËùʾ£º

DELETE FROM customerinfo
WHERE CustomerID NOT in (SELECT CustomerID FROM salesinfo )

¡¡¡¡Ê¹ÓÃ×Ó²éѯ¿ÉÒÔÒ»´ÎÐÔµÄÍê³ÉºÜ¶àÂß¼­ÉÏÐèÒª¶à¸ö²½Öè²ÅÄÜÍê³ÉµÄSQL²Ù×÷£¬Í¬Ê±Ò²¿ÉÒÔ±ÜÃâÊÂÎñ»òÕß±íËøËÀ£¬²¢ÇÒдÆðÀ´Ò²ºÜÈÝÒס£µ«ÊÇ£¬ÓÐЩÇé¿öÏ£¬×Ó²éѯ¿ÉÒÔ±»¸üÓÐЧÂʵÄÁ¬½Ó£¨JOIN£©.. Ìæ´ú¡£ÀýÈ磬¼ÙÉèÎÒÃÇÒª½«ËùÓÐûÓж©µ¥¼Ç¼µÄÓû§È¡³öÀ´£¬¿ÉÒÔÓÃÏÂÃæÕâ¸ö²éѯÍê³É£º

SELECT * FROM customerinfo
WHERE CustomerID NOT in (SELECT CustomerID FROM salesinfo )

¡¡¡¡Èç¹ûʹÓÃÁ¬½Ó£¨JOIN£©.. À´Íê³ÉÕâ¸ö²éѯ¹¤×÷£¬ËٶȽ«»á¿ìºÜ¶à¡£ÓÈÆäÊǵ±salesinfo±íÖжÔCustomerID½¨ÓÐË÷ÒýµÄ»°£¬ÐÔÄܽ«»á¸üºÃ£¬²éѯÈçÏ£º

SELECT * FROM customerinfo
LEFT JOIN salesinfoON customerinfo.CustomerID=salesinfo.
CustomerID
WHERE salesinfo.CustomerID IS NULL

¡¡¡¡Á¬½Ó£¨JOIN£©.. Ö®ËùÒÔ¸üÓÐЧÂÊһЩ£¬ÊÇÒòΪ MySQL²»ÐèÒªÔÚÄÚ´æÖд´½¨ÁÙʱ±íÀ´Íê³ÉÕâ¸öÂß¼­ÉϵÄÐèÒªÁ½¸ö²½ÖèµÄ²éѯ¹¤×÷¡£

¡¡¡¡3¡¢Ê¹ÓÃÁªºÏ(UNION)À´´úÌæÊÖ¶¯´´½¨µÄÁÙʱ±í

¡¡¡¡MySQL ´Ó 4.0 µÄ°æ±¾¿ªÊ¼Ö§³Ö UNION ²éѯ£¬Ëü¿ÉÒÔ°ÑÐèҪʹÓÃÁÙʱ±íµÄÁ½Ìõ»ò¸ü¶àµÄ SELECT ²éѯºÏ²¢µÄÒ»¸ö²éѯÖС£ÔÚ¿Í»§¶ËµÄ²éѯ»á»°½áÊøµÄʱºò£¬ÁÙʱ±í»á±»×Ô¶¯É¾³ý£¬´Ó¶ø±£Ö¤Êý¾Ý¿âÕûÆë¡¢¸ßЧ¡£Ê¹Óà UNION À´´´½¨²éѯµÄʱºò£¬ÎÒÃÇÖ»ÐèÒªÓà UNION×÷Ϊ¹Ø¼ü×ְѶà¸ö SELECT Óï¾äÁ¬½ÓÆðÀ´¾Í¿ÉÒÔÁË£¬Òª×¢ÒâµÄÊÇËùÓÐ SELECT Óï¾äÖеÄ×Ö¶ÎÊýÄ¿ÒªÏëͬ¡£ÏÂÃæµÄÀý×Ó¾ÍÑÝʾÁËÒ»¸öʹÓà UNIONµÄ²éѯ¡£

SELECT Name, Phone FROM client
UNION
SELECT Name, BirthDate FROM author
UNION
SELECT Name, Supplier FROM product

¡¡¡¡4¡¢ÊÂÎñ

¡¡¡¡¾¡¹ÜÎÒÃÇ¿ÉÒÔʹÓÃ×Ó²éѯ£¨Sub-Queries£©¡¢Á¬½Ó£¨JOIN£©ºÍÁªºÏ£¨UNION£©À´´´½¨¸÷ÖÖ¸÷ÑùµÄ²éѯ£¬µ«²»ÊÇËùÓеÄÊý¾Ý¿â²Ù×÷¶¼¿ÉÒÔÖ»ÓÃÒ»Ìõ»òÉÙÊý¼¸ÌõSQLÓï¾ä¾Í¿ÉÒÔÍê³ÉµÄ¡£¸ü¶àµÄʱºòÊÇÐèÒªÓõ½Ò»ÏµÁеÄÓï¾äÀ´Íê³ÉijÖÖ¹¤×÷¡£µ«ÊÇÔÚÕâÖÖÇé¿öÏ£¬µ±Õâ¸öÓï¾ä¿éÖеÄijһÌõÓï¾äÔËÐгö´íµÄʱºò£¬Õû¸öÓï¾ä¿éµÄ²Ù×÷¾Í»á±äµÃ²»È·¶¨ÆðÀ´¡£ÉèÏëһϣ¬Òª°Ñij¸öÊý¾Ýͬʱ²åÈëÁ½¸öÏà¹ØÁªµÄ±íÖУ¬¿ÉÄÜ»á³öÏÖÕâÑùµÄÇé¿ö£ºµÚÒ»¸ö±íÖгɹ¦¸üкó£¬Êý¾Ý¿âͻȻ³öÏÖÒâÍâ×´¿ö£¬Ôì³ÉµÚ¶þ¸ö±íÖеIJÙ×÷ûÓÐÍê³É£¬ÕâÑù£¬¾Í»áÔì³ÉÊý¾ÝµÄ²»ÍêÕû£¬ÉõÖÁ»áÆÆ»µÊý¾Ý¿âÖеÄÊý¾Ý¡£Òª±ÜÃâÕâÖÖÇé¿ö£¬¾ÍÓ¦¸ÃʹÓÃÊÂÎñ£¬ËüµÄ×÷ÓÃÊÇ£ºÒªÃ´Óï¾ä¿éÖÐÿÌõÓï¾ä¶¼²Ù×÷³É¹¦£¬ÒªÃ´¶¼Ê§°Ü¡£»»¾ä»°Ëµ£¬¾ÍÊÇ¿ÉÒÔ±£³ÖÊý¾Ý¿âÖÐÊý¾ÝµÄÒ»ÖÂÐÔºÍÍêÕûÐÔ¡£ÊÂÎïÒÔBEGIN ¹Ø¼ü×Ö¿ªÊ¼£¬COMMIT¹Ø¼ü×Ö½áÊø¡£ÔÚÕâÖ®¼äµÄÒ»ÌõSQL²Ù×÷ʧ°Ü£¬ÄÇô£¬ROLLBACKÃüÁî¾Í¿ÉÒÔ°ÑÊý¾Ý¿â»Ö¸´µ½BEGIN¿ªÊ¼Ö®Ç°µÄ״̬¡£

    ºÚ¿Í·ÀÏßÍø°²·þÎñÆ÷ά»¤·½°¸±¾ÆªÁ¬½Ó£ºhttp://www.rongsen.com.cn/show-7930-1.html
Íøվά»¤½Ì³Ì¸üÐÂʱ¼ä:2012-03-20 04:45:30  ¡¾´òÓ¡´ËÒ³¡¿  ¡¾¹Ø±Õ¡¿
ÎÒÒªÉêÇë±¾Õ¾£ºNµã | ºÚ¿Í·ÀÏß¹ÙÍø |  
רҵ·þÎñÆ÷ά»¤¼°Íøվά»¤ÊÖ¹¤°²È«´î½¨»·¾³£¬ÍøÕ¾°²È«¼Ó¹Ì·þÎñ¡£ºÚ¿Í·ÀÏßÍø°²·þÎñÆ÷ά»¤»ùµØÕÐÉ̽øÐÐÖУ¡QQ:29769479

footer  footer  footer  footer