mysql Á´½Ó²éѯ
inner joins 內Á¬½Ó½öÑ¡³öÁ½ÕűíÖл¥ÏàÆ¥ÅäµÄ¼Ç¼£®Òò´Ë£¬Õâ»áµ¼ÖÂÓÐʱÎÒÃÇÐèÒªµÄ¼Ç¼ûÓаüº¬½øÀ´¡£
Ϊ¸üºÃµÄÀí½âÕâ¸ö¸ÅÄÎÒÃǽéÉÜÁ½¸ö±í×÷ÑÝʾ¡£ËÕ¸ñÀ¼Òé»áÖеÄÕþµ³±í(party)ºÍÒéÔ±±í(msp)¡£
party(Co
de,Name,Leader)
Code: Õþµ³´úÂë
Name: Õþµ³Ãû³Æ
Leader: Õþµ³ÁìÐä
msp(Name,Party,Constituency)
Name: ÒéÔ±Ãû
Party: ÒéÔ±ËùÔÚÕþµ³´úÂë
Constituency: Ñ¡Çø
ÔÚ½éÉÜ×óÁ¬½Ó¡¢ÓÒÁ¬½ÓºÍÈ«Á¬½ÓÇ°£¬ÓÐÒ»¸öÊý¾Ý¿âÖÐÖØÒªµÄ¸ÅÄîÒª½éÉÜһϣ¬¼´¿ÕÖµ(NULL)¡£
A left join£¨×óÁ¬½Ó£©°üº¬ËùÓеÄ×ó±ß±íÖеļǼÉõÖÁÊÇÓұ߱íÖÐûÓкÍËüÆ¥ÅäµÄ¼Ç¼¡£
ͬÀí£¬Ò²´æÔÚ×ÅÏàͬµÀÀíµÄ right join£¨ÓÒÁ¬½Ó£©£¬¼´°üº¬ËùÓеÄÓұ߱íÖеļǼÉõÖÁÊÇ×ó±ß±íÖÐûÓкÍËüÆ¥ÅäµÄ¼Ç¼¡£
¶øfull join(È«Á¬½Ó)¹ËÃû˼Ò壬×óÓÒ±íÖÐËùÓмǼ¶¼»áÑ¡³öÀ´¡£
½²µ½ÕâÀÓÐÈË¿ÉÄÜÒªÎÊ£¬µ½µ×ʲô½Ð£º°üº¬ËùÓеÄ×ó±ß±íÖеļǼÉõÖÁÊÇÓұ߱íÖÐûÓкÍËüÆ¥ÅäµÄ¼Ç¼¡£
Ok,ÎÒÃÇÀ´¿´Ò»¸öʵÀý£º
SELECT msp.name, party.name
from msp JOIN party ON party=code
ÄÚÁ¬½Ó
1.¸ÅÄÄÚÁª½ÓÊÇÓñȽÏÔËËã·û±È½ÏÒªÁª½ÓÁеÄÖµµÄÁª½Ó
2.ÄÚÁ¬½Ó£ºjoin »ò inner join
ºÜÒź¶£¬ÎÒÃÇ·¢ÏָòéѯµÄ½á¹ûÉÙÁËÁ½¸öÒéÔ±£ºCanavan MSP, Dennis¡£ÎªÊ²Ã´£¬ÒòΪÕâÁ½¸öÒéÔ±²»ÊôÓÚÈÎ
ºÍÕþµ³£¬¼´ËûÃǵÄÕþµ³×Ö¶Î(Party)Ϊ¿ÕÖµ¡£ÄÇôΪʲô²»ÊôÓÚÈκÎÕþµ³¾Í²é²»³öÀ´ÁË£¿ÕâÊÇÒòΪ¿ÕÖµÔÚ
×÷¹Ö¡£ÒòΪÒéÔ±±íÖÐÕþµ³×Ö¶Î(Party)µÄ¿ÕÖµÔÚÕþµ³±íÖÐÕÒ²»µ½¶ÔÓ¦µÄ¼Ç¼×÷Æ¥Å䣬¼´
from msp JOIN party ON party=code¡¡Ã»ÓаѸüǼÁ¬½ÓÆðÀ´£¬¶øÊǹýÂ˳öÈ¥ÁË¡£
ÔڸöÌÓïÖУ¬mspÔÚJoinµÄ×ó±ß£¬ËùÓгÆΪ×ó±í¡£partyÔÚJoinµÄÓұߣ¬ËùÓгÆΪÓÒ±í¡£
Ok,ÏÖÔÚÔÙ¿´¿´Õâ¾ä»°£¬“°üº¬ËùÓеÄ×ó±ß±íÖеļǼÉõÖÁÊÇÓұ߱íÖÐûÓкÍËüÆ¥ÅäµÄ¼Ç¼”£¬
Òâ˼Ӧ¸ÃºÜÃ÷°×ÁË°É¡£Ö´ÐÐÏÂÃæÕâ¸öÓï¾ä£¬ÄÇÁ½¸öûÓÐÕþµ³µÄÒéÔ±¾Í©²»ÁËÁË¡£
SELECT msp.name, party.name
from msp LEFT JOIN party ON party=code
¹ØÓÚÓÒÁ¬½Ó,¿´¿´Õâ¸ö²éѯ¾ÍÃ÷°×ÁË£º
SELECT msp.name, party.name
from msp RIGHT JOIN party ON msp.party=party.code
Õâ¸ö²éѯµÄ½á¹ûÁгöËùÓеÄÒéÔ±ºÍÕþµ³£¬°üº¬Ã»ÓÐÒéÔ±µÄÕþµ³£¬µ«²»°üº¬Ã»ÓÐÕþµ³µÄÒéÔ±¡£
ÄÇô¼ÈÒª°üº¬Ã»ÓÐÒéÔ±µÄÕþµ³£¬ÓÖÒª°üº¬Ã»ÓÐÕþµ³µÄÒéÔ±¸ÃÔõô°ìÄØ£¬¶ÔÁË£¬È«Á¬½Ó(full join)
Ïà¹ØÎĵµ£º
ÔÚ MySQLÏ£¬ÔÚ½øÐÐÖÐÎÄÄ£ºý¼ìË÷ʱ£¬¾³£»á·µ»ØһЩÓëÖ®²»Ïà¹ØµÄ¼Ç¼£¬Èç²éÕÒ "%a%" ʱ£¬·µ»ØµÄ¿ÉÄÜÓÐÖÐÎÄ×Ö·û£¬È´Ã»ÓÐa×Ö·û´æÔÚ¡£±¾ÈËÒÔÇ°Ò²ÔøÓöµ½¹ýÀàËÆÎÊÌ⣬¾ÏêϸÔĶÁMySQLµÄManual£¬·¢ÏÖ¿ÉÒÔÓÐÒ»ÖÖ·½·¨ºÜ·½±ãµÄ½â¾ö²¢µÃµ½ÂúÒâµÄ½á¹û¡£
¡¡¡¡Àý×Ó£º
¡¡¡¡Ï£Íûͨ¹ý“±êÌ┶ÔÐÂÎÅ¿â½øÐмìË÷£¬¹Ø¼ü×Ö¿ÉÄÜ°üº¬Ê ......
1¡¢Íâ¼üµÄÒýÓÃÀàÐͲ»Ò»Ñù£¬Ö÷¼üÊÇintÍâ¼üÊÇchar
2¡¢ÕÒ²»µ½Ö÷±íÖÐ ÒýÓõÄÁÐ
3¡¢Ö÷¼üºÍÍâ¼üµÄ×Ö·û±àÂë²»Ò»ÖÂ
4.»¹ÓÐÒª½¨Á¢Íâ¼üµÄ»°£¬ÒªÏȽ¨Á¢Ë÷Òý¡£Ã»Óн¨Á¢Ë÷ÒýÒ²»á³ö´í¡£
ÎÒµÄÎÊÌâ½â¾ö·½°¸ÊÇÔÚsqlºóÃæ¼ÓÉÏÁËÈçϾ仰,ok!³É¹¦µ¼Èë½Å±¾
ENGINE=MyISAM DEFAULT CHARSET=utf8;
charset¶ÔÓ¦µÄ»»³ÉÄãµÄÖ÷¼ü±íµÄ×Ö·û¼¯ ......
ͨ¹ýMySQLÄÚÖÃÈ«ÎļìË÷ʵÏÖÖÐÎĵÄÏà¹Ø¼ìË÷
¹Ø¼ü×Ö£ºMySQL È«ÎļìË÷ È«ÎÄË÷Òý ÖÐÎÄ·Ö´Ê ¶þÔª·Ö´Ê ÇøλÂë ÏàËƶÈ
×¢£º±¾ÎÄʹÓõÄMySQL°æ±¾Îª£ºMySQL 4.0.x
ÔÚMySQL4ÖУ¬ÊÇÒѾ¿ªÊ¼Ö§³ÖÈ«ÎļìË÷£¨Ë÷Òý£©µÄÁË¡£µ«ÊÇÖ»ÊǶÔÓ¢ÎÄÖ§³ÖÈ«ÎļìË÷¡£
ÓÉÓÚÓ¢ÎÄÔÚÊéдÉϵÄÌØÊâÐÔ£¬Ê¹µÃ·Ö´ÊËã·¨Ïà¶ÔÖÐÎÄÀ´Ëµ£¬¼òµ¥µÃ¶à¡£Ò»°ãÀ´Ëµ£¬ÎÒÃÇ ......
¸ã¶¨MySQLÊý¾Ý¿âÖÐÎÄÄ£ºý¼ìË÷ÎÊÌâ
×÷Õß: bianceng, ¡¡³ö´¦:ITר¼ÒÍøÂÛ̳,¡¡ÔðÈαà¼: ³Â×Óç÷,¡¡
2009-10-28 07:00
¡¡¡¡ÔÚ MySQLÏ£¬ÔÚ½øÐÐÖÐÎÄÄ£ºý¼ìË÷ʱ£¬¾³£»á·µ»ØһЩÓëÖ®²»Ïà¹ØµÄ¼Ç¼£¬Èç²éÕÒ "%a%" ʱ£¬·µ»ØµÄ¿ÉÄÜÓÐÖÐÎÄ×Ö·û£¬È´Ã»ÓÐa×Ö·û´æÔÚ¡£
¡¡¡¡ÔÚ MySQLÏ£¬ÔÚ½øÐÐÖÐÎÄÄ£ºý¼ìË÷ʱ£¬¾³£»á·µ»ØһЩÓë ......