MYSQl left joinÁªºÏ²éѯЧÂÊ·ÖÎö
user±í:
id | name
———
1 | libk
2 | zyfon
3 | daodao
user_action±í:
user_id | action
—————
1 | jump
1 | kick
1 | jump
2 | run
4 | swim
sql:
select id, name, action from user as u
left join user_action a on u.id = a.user_id
result:
id | name | action
——————————–
1 | libk | jump ¢Ù
1 | libk | kick ¢Ú
1 | libk | jump ¢Û
2 | zyfon | run ¢Ü
3 | daodao | null ¢Ý
·ÖÎö£º
×¢Òâµ½user_actionÖл¹ÓÐÒ»¸öuser_id=4, action=swimµÄ¼Í¼£¬µ«ÊÇûÓÐÔÚ½á¹ûÖгöÏÖ£¬
¶øuser±íÖеÄid=3, name=daodaoµÄÓû§ÔÚuser_actionÖÐûÓÐÏàÓ¦µÄ¼Í¼£¬µ«ÊÇÈ´³öÏÖÔÚÁ˽á¹û¼¯ÖÐ
ÒòΪÏÖÔÚÊÇleft join£¬ËùÓеŤ×÷ÒÔleftΪ׼.
½á¹û1£¬2£¬3£¬4¶¼ÊǼÈÔÚ×ó±íÓÖÔÚÓÒ±íµÄ¼Í¼£¬5ÊÇÖ»ÔÚ×ó±í£¬²»ÔÚÓÒ±íµÄ¼Í¼
½áÂÛ£º
ÎÒÃÇ¿ÉÒÔÏëÏóleft join ÊÇÕâÑù¹¤×÷µÄ
´Ó×ó±í¶Á³öÒ»Ìõ£¬Ñ¡³öËùÓÐÓëonÆ¥ÅäµÄÓÒ±í¼Í¼(nÌõ)½øÐÐÁ¬½Ó£¬ÐγÉnÌõ¼Í¼(°üÀ¨Öظ´µÄÐУ¬È磺½á¹û1ºÍ½á¹û3)£¬
Èç¹ûÓÒ±ßûÓÐÓëonÌõ¼þÆ¥ÅäµÄ±í£¬ÄÇÁ¬½ÓµÄ×ֶζ¼ÊÇnull.
È»ºó¼ÌÐø¶ÁÏÂÒ»Ìõ¡£
ÒýÉ꣺
ÎÒÃÇ¿ÉÒÔÓÃÓÒ±íûÓÐonÆ¥ÅäÔòÏÔʾnullµÄ¹æÂÉ, À´ÕÒ³öËùÓÐÔÚ×ó±í£¬²»ÔÚÓÒ±íµÄ¼Í¼£¬ ×¢ÒâÓÃÀ´ÅжϵÄÄÇÁбØÐëÉùÃ÷Ϊnot nullµÄ¡£
È磺
sql:
select id, name, action from user as u
left join user_action a on u.id = a.user_id
where a.user_id is NULL
(×¢Òâ:1.ÁÐֵΪnullÓ¦¸ÃÓÃis null ¶ø²»ÄÜÓÃ=NULL
2.ÕâÀïa.user_id ÁбØÐëÉùÃ÷Ϊ NOT NULL µÄ)
result:
id | name | action
————————–
3 | daodao | NULL
——————————————————————————–
Tips:
1. on a.c1 = b.c1 µÈͬÓÚ using(c1)
2. INNER JOIN ºÍ , (¶ººÅ) ÔÚÓïÒåÉÏÊǵÈͬµÄ
3. µ± MySQL ÔÚ´ÓÒ»¸ö±íÖмìË÷ÐÅϢʱ£¬Äã¿ÉÒÔÌáʾËüÑ¡ÔñÁËÄÄÒ»¸öË÷Òý¡£
Èç¹û EXPLAIN ÏÔʾ MySQL ʹÓÃÁË¿ÉÄܵÄË÷ÒýÁбíÖдíÎóµÄË÷Òý£¬Õâ¸öÌØÐÔ½«ÊǺÜÓÐÓõġ£
ͨ¹ýÖ¸¶¨ USE INDEX (key_list)£¬Äã¿ÉÒÔ¸æËß MySQL ʹÓÿÉÄܵÄË÷ÒýÖÐ×îºÏÊʵÄÒ»¸öË÷ÒýÔÚ±íÖвéÕҼǼÐС£
¿ÉÑ¡µ
Ïà¹ØÎĵµ£º
Ò»¡¢Ê²Ã´ÊÇË÷Òý£¿
Ë÷ÒýÓÃÀ´¿ìËÙµØÑ°ÕÒÄÇЩ¾ßÓÐÌض¨ÖµµÄ¼Ç¼£¬ËùÓÐMySQLË÷Òý¶¼ÒÔB-Ê÷µÄÐÎʽ±£´æ¡£Èç¹ûûÓÐË÷Òý£¬Ö´ÐвéѯʱMySQL±ØÐë´ÓµÚÒ»¸ö¼Ç¼¿ªÊ¼É¨ÃèÕû¸ö±íµÄËùÓмǼ£¬Ö±ÖÁÕÒµ½·ûºÏÒªÇóµÄ¼Ç¼¡£±íÀïÃæµÄ¼Ç¼ÊýÁ¿Ô½¶à£¬Õâ¸ö²Ù×÷µÄ´ú¼Û¾ÍÔ½¸ß¡£Èç¹û×÷ΪËÑË÷Ìõ¼þµÄÁÐÉÏÒѾ´´½¨ÁËË÷Òý£¬MySQLÎÞÐèɨÃèÈκμǼ¼´¿ÉѸËٵà ......
ͨ¹ýMySQLÄÚÖÃÈ«ÎļìË÷ʵÏÖÖÐÎĵÄÏà¹Ø¼ìË÷
¹Ø¼ü×Ö£ºMySQL È«ÎļìË÷ È«ÎÄË÷Òý ÖÐÎÄ·Ö´Ê ¶þÔª·Ö´Ê ÇøλÂë ÏàËƶÈ
×¢£º±¾ÎÄʹÓõÄMySQL°æ±¾Îª£ºMySQL 4.0.x
ÔÚMySQL4ÖУ¬ÊÇÒѾ¿ªÊ¼Ö§³ÖÈ«ÎļìË÷£¨Ë÷Òý£©µÄÁË¡£µ«ÊÇÖ»ÊǶÔÓ¢ÎÄÖ§³ÖÈ«ÎļìË÷¡£
ÓÉÓÚÓ¢ÎÄÔÚÊéдÉϵÄÌØÊâÐÔ£¬Ê¹µÃ·Ö´ÊËã·¨Ïà¶ÔÖÐÎÄÀ´Ëµ£¬¼òµ¥µÃ¶à¡£Ò»°ãÀ´Ëµ£¬ÎÒÃÇ ......
inner joins 內Á¬½Ó½öÑ¡³öÁ½ÕűíÖл¥ÏàÆ¥ÅäµÄ¼Ç¼£®Òò´Ë£¬Õâ»áµ¼ÖÂÓÐʱÎÒÃÇÐèÒªµÄ¼Ç¼ûÓаüº¬½øÀ´¡£
Ϊ¸üºÃµÄÀí½âÕâ¸ö¸ÅÄÎÒÃǽéÉÜÁ½¸ö±í×÷ÑÝʾ¡£ËÕ¸ñÀ¼Òé»áÖеÄÕþµ³±í(party)ºÍÒéÔ±±í(msp)¡£
party(Co
de,Name,Leader)
Code: Õþµ³´úÂë
Name: Õþµ³Ãû³Æ
Leader: Õþµ³ÁìÐä
msp(Name,Party,Constituency ......
Ë÷ÒýÊÇÌá¸ß²éѯËٶȵÄ×îÖØÒªµÄ¹¤¾ß¡£µ±È»»¹ÓÐÆäËüµÄһЩ¼¼Êõ¿É¹©Ê¹Ó㬵«ÊÇÒ»°ãÀ´ËµÒýÆð×î´óÐÔÄܲîÒìµÄ¶¼ÊÇË÷ÒýµÄÕýȷʹÓá£ÔÚMySQLÓʼþÁбíÖУ¬ÈËÃÇ
¾³£Ñ¯ÎÊÄÇЩÈòéѯÔËÐеøü¿ìµÄ·½·¨¡£ÔÚ´ó¶àÊýÇé¿öÏ£¬ÎÒÃÇÓ¦¸Ã»³ÒÉÊý¾Ý±íÉÏÓÐûÓÐË÷Òý£¬²¢ÇÒͨ³£ÔÚÌí¼ÓË÷ÒýÖ®ºóÁ¢¼´½â¾öÁËÎÊÌâ¡£µ±È»£¬²¢²»×ÜÊÇÕâÑù¼òµ¥
¾Í¿ÉÒÔ½ ......
ÔÊÐíËùÓÐÖ÷»úÔ¶³Ì·ÃÎÊ
GRANT ALL PRIVILEGES ON *.* TO 'dba'@'%' IDENTIFIED BY 'dbapassword' WITH GRANT OPTION;
Ö¸¶¨Ö÷»ú¿ÉÔ¶³Ì
GRANT ALL PRIVILEGES ON *.* TO 'dba'@'192.168.0.100' IDENTIFIED BY 'dbapassword' WITH GRANT OPTION;
Ö¸¶¨È¨ÏÞ¿ªÔ¶³Ì
grant select,insert,update,delete on test.* to db ......