SQLÖÐJOINºÍUNIONÇø±ð¡¢Ó÷¨¼°Ê¾Àý
1.JOINºÍUNIONÇø±ð
join ÊÇÁ½Õűí×ö½»Á¬ºóÀïÃæÌõ¼þÏàͬµÄ²¿·Ö¼Ç¼²úÉúÒ»¸ö¼Ç¼¼¯£¬
unionÊDzúÉúµÄÁ½¸ö¼Ç¼¼¯(×Ö¶ÎÒªÒ»ÑùµÄ)²¢ÔÚÒ»Æ𣬳ÉΪһ¸öеļǼ¼¯ ¡£
JOINÓÃÓÚ°´ÕÕONÌõ¼þÁª½ÓÁ½¸ö±í£¬Ö÷ÒªÓÐËÄÖÖ£º
INNER JOIN£ºÄÚ²¿Áª½ÓÁ½¸ö±íÖеļǼ£¬½öµ±ÖÁÉÙÓÐÒ»¸öͬÊôÓÚÁ½±íµÄÐзûºÏÁª½ÓÌõ¼þʱ£¬ÄÚÁª½Ó²Å·µ»ØÐС£ÎÒÀí½âµÄÊÇÖ»Òª¼Ç¼²»·ûºÏONÌõ¼þ£¬¾Í²»»áÏÔʾÔÚ½á¹û¼¯ÄÚ¡£
LEFT JOIN / LEFT OUTER JOIN£ºÍⲿÁª½ÓÁ½¸ö±íÖеļǼ£¬²¢°üº¬×ó±íÖеÄÈ«²¿¼Ç¼¡£Èç¹û×ó±íµÄij¼Ç¼ÔÚÓÒ±íÖÐûÓÐÆ¥Åä¼Ç¼£¬ÔòÔÚÏà¹ØÁªµÄ½á¹û¼¯ÖÐÓÒ±íµÄËùÓÐÑ¡ÔñÁбíÁоùΪ¿ÕÖµ¡£Àí½âΪ¼´Ê¹²»·ûºÏONÌõ¼þ£¬×ó±íÖеļǼҲȫ²¿ÏÔʾ³öÀ´£¬ÇÒ½á¹û¼¯ÖиÃÀà¼Ç¼µÄÓÒ±í×Ö¶ÎΪ¿ÕÖµ¡£
RIGHT JOIN / RIGHT OUTER JOIN£ºÍⲿÁª½ÓÁ½¸ö±íÖеļǼ£¬²¢°üº¬ÓÒ±íÖеÄÈ«²¿¼Ç¼¡£¼òµ¥Ëµ¾ÍÊǺÍLEFT JOIN·´¹ýÀ´¡£
FULL JOIN / FULL OUTER JOIN£ºÍêÕûÍⲿÁª½Ó·µ»Ø×ó±íºÍÓÒ±íÖеÄËùÓÐÐС£¾ÍÊÇLEFT JOINºÍRIGHT JOINºÍºÏ²¢£¬×óÓÒÁ½±íµÄÊý¾Ý¶¼È«²¿ÏÔʾ¡£
JOINµÄ»ù±¾Óï·¨£º
Select table1.* from table1 JOIN table2 ON table1.id=table2.id
sqlд·¨
ÄÚÁ¬½Óinner join£º
SELECT msp.name, party.name
from msp JOIN party ON party=code
»ò
SELECT msp.name, party.name
from msp inner JOIN party ON party=code
×óÁ¬½Óleft join £º
SELECT msp.name, party.name
from msp LEFT JOIN party ON party=code
ÓÒÁ¬½Óright join £º
SELECT msp.name, party.name
from msp RIGHT JOIN party ON msp.party=party.code
È«Á¬½Ó(full join)£º
SELECT msp.name, party.name
from msp FULL JOIN party ON msp.party=party.code
UNIONÔËËã·û
½«Á½¸ö»ò¸ü¶à²éѯµÄ½á¹û¼¯×éºÏΪµ¥¸ö½á¹û¼¯£¬¸Ã½á¹û¼¯°üº¬ÁªºÏ²éѯÖеÄËùÓвéѯµÄÈ«²¿ÐС£UNIONµÄ½á¹û¼¯ÁÐÃûÓëUNIONÔËËã·ûÖеÚÒ»¸öSelectÓï¾äµÄ½á¹û¼¯µÄÁÐÃûÏàͬ¡£ÁíÒ»¸öSelectÓï¾äµÄ½á¹û¼¯ÁÐÃû½«±»ºöÂÔ¡£
ÆäÖÐÁ½ÖÖ²»Í¬µÄÓ÷¨ÊÇUNIONºÍUNION ALL£¬Çø±ðÔÚÓÚUNION´Ó½á¹û¼¯ÖÐɾ³ýÖظ´µÄÐС£Èç¹ûʹÓÃUNION ALL ½«°üº¬ËùÓÐÐв¢ÇÒ½«²»É¾³ýÖظ´µÄÐС£
UNIONºÍUNION ALLµÄÇø±ð£º
union ¼ì²éÖظ´
union all ²»×ö¼ì²é
±ÈÈç select 'a' union select 'a' Êä³ö¾ÍÊÇÒ»ÐÐ a
±ÈÈç select 'a' union all select 'a' Êä³ö¾ÍÊÇÁ½ÐÐ a
2. ͨ¹ýÏÂÃæµÄÀý×Ó£¬¿ÉÒÔÇåÎúµÄ¿´³öºÍÀí½â2ÕßµÄÇø±ð
ʵÀý1 µäÐ͵Ķþ±íÁ¬½ÓÑÝʾ
¼Ù¶¨ÓÐÁ½¸ö±íTable1ºÍTable2£¬Æä°üº¬µÄÁкÍÊý¾Ý·Ö
Ïà¹ØÎĵµ£º
ϵͳ»·¾³£ºWindows 7
Èí¼þ»·¾³£ºVisual C++ 2008 SP1 +SQL Server 2005
±¾´ÎÄ¿µÄ£º±àдһ¸öº½¿Õ¹ÜÀíϵͳ
ÕâÊÇÊý¾Ý¿â¿Î³ÌÉè¼ÆµÄ³É¹û£¬ËäÈ»³É¼¨²»¼Ñ£¬µ«ÊÇ×÷ΪÎÒÓÃVC++ ÒÔÀ´±àдµÄ×î´ó³ÌÐò»¹ÊÇ´«µ½ÍøÉÏ£¬ÒÔ¹©²Î¿¼¡£ÓÃVC++ ×öÊý¾Ý¿âÉè¼Æ²¢²»ÈÝÒ×£¬µ«Ò²²»ÊDz»¿ÉÄÜ¡£ÒÔÏÂÊÇÎҵijÌÐò½çÃ棬ºóÃæ ......
1. ÔÚʹÓÃsql server ֮ǰÄØ£¬×îºÃ¶ÔÆä½øÐÐÅäÖÃ
(1) ÅäÖù¤¾ß->Sql server configuration Manager
Ñ¡ÔñSql server 2005Íø·ÅäÖà Ï嵀 MSSQLSERVERµÄÐÒé ÔÚ´°¿ÚÓұߵÄ
TCP/IPÐÒéÓÒ¼üÊôÐÔ£¬ÅäÖÃÈçÏÂ
&n ......
ÆäʵÄã¿ÉÒÔʹÓÃÊÂÎñ´¦Àí
±È·½ËµÔÚÒ»¸ö×Ö¶ÎÀïÃæÌí¼ÓÒ»¸öboolean µÄ×ֶε±ÄãÒª´¦Àí¸Ã×ֶεÄʱºò¾Í True ÄÄô±ðµÄÈ˶¼²»¿ÉÒÔ½øÐвÙ×÷
¡¡Èç¹ûÊÇFalse ÄÄô¾Í¿ÉÒÔ½øÐвÙ×÷¡«¡«ºÇ¿É¡«¡«ÎÒÊÇÕâÑùµÄ
¡¡¡¡²»¹ýÄã¿ÉÒÔ¿´¿´¡¡
¡¡SQLËø»úÖÆ
¡¡¡¡ÒÔÏÂÊÇÎÒÕÒµÄһЩÁÏ¡¡Èç¹ûÒªÀí½âSQLËø»úÖÆ ×îºÃ°ÑÏÂÀ´¿´ÍêŶ
ĬÈϵÄÊÂÎñ¸ôÀë¼ ......
ÔÚʹÓÃSQL ServerÊý¾Ý¿âʱ, Èç¹ûÐèÒª·ÃÎÊÔ¶³ÌµÄÁíÍâÒ»¸öÊý¾Ý¿â, ÄÇô¿ÉÒÔʹÓÃLinked ServerÀ´ÊµÏÖÕâ¸ö¹¦ÄÜ, ºÜ·½±ã. ±¾ÎÄ×ܽáÁË×Ô¼ºÔÚʹÓÃLinked Server¹¦ÄÜʱÓöµ½µÄ¼¸¸öÎÊÌâ, Èç¹ûÉÔ²»×¢Òâ,ºÜ¿ÉÄܳöÏÖ¸÷ÖÖ´íÎó.
1)´´½¨ÁËÒ»¸öеÄLinked Serverºó, ÐèÒªÊ×ÏÈÉèÖÃlogin ÕË»§, ÓÒ¼ü->ÊôÐÔ->Security, Èçͼ
......
inner join(µÈÖµÁ¬½Ó) Ö»·µ»ØÁ½¸ö±íÖÐÁª½á×Ö¶ÎÏàµÈµÄÐÐ
left join(×óÁª½Ó) ·µ»Ø°üÀ¨×ó±íÖеÄËùÓмǼºÍÓÒ±íÖÐÁª½á×Ö¶ÎÏàµÈµÄ¼Ç¼
right join(ÓÒÁª½Ó) ·µ»Ø°üÀ¨ÓÒ±íÖеÄËùÓмǼºÍ×ó±íÖÐÁª½á×Ö¶ÎÏàµÈµÄ¼Ç¼
INNER JOIN Óï·¨£º
INNER JOIN Á¬½ÓÁ½¸öÊý¾Ý±íµÄÓ÷¨£º
SELECT * from ±í1 INNER JOIN ±í2 ON ±í1.×ֶκÅ=±í2 ......