sqlÖÐunionµÄ×÷ÓÃ
union ÔËËã·û½«¶à¸ö±íµÄ¾ßÓÐÏàͬÁнṹµÄÐкϲ¢ÎªÒ»¸öЧ¹û¼¯£¬Ïû³ýÖظ´Ðв¢×Ô¶¯×ª»»Êý¾ÝÀàÐÍ¡£1. ºÏ²¢À´×Ô²î±ð±íµÄÐУ»2. Ïû³ýÖظ´ÐУ»3. ת»»Êý¾ÝÀàÐÍÒÔÆ¥ÅäÁнṹ¡£
SQL ÖÐ UNION µÄ×÷ÓÃ
UNION ÔËËã·ûÓÃÓںϲ¢À´×ÔÁ½¸ö»ò¶à¸ö±íµÄ¾ßÓÐÏàͬÁнṹµÄÐУ¬ÐγÉÒ»¸öеÄЧ¹û¼¯¡£
ʹÓÃÒªÁ죺
UNION µÄÓï·¨ÈçÏ£º
SELECT ÁÐÃû1, ÁÐÃû2, ... FROM ±íÃû1 UNION SELECT ÁÐÃû1, ÁÐÃû2, ... FROM ±íÃû2
µÇ¼ºó¸´ÖÆ
×÷Óãº
ºÏ²¢ÐУº½«¶à¸ö±íµÄÐкϲ¢µ½Ò»Æð£¬ÐγÉÒ»¸öеÄЧ¹û¼¯¡£
Ïû³ýÖظ´ÐУºÄ¬ÈÏÇéÐÎÏ£¬UNION »áÏû³ýÖظ´ÐС£
²î±ð±í£º¿ÉºÏ²¢À´×Ô²î±ð±íµÄÐУ¬Ö»ÒªËüÃǾßÓÐÏàͬµÄÁнṹ¡£
Êý¾ÝÀàÐÍת»»£ºUNION »á×Ô¶¯½«²î±ðÊý¾ÝÀàÐ͵ÄÁÐת»»Îª¼æÈݵÄÊý¾ÝÀàÐÍ¡£
×¢ÖØÊÂÏ
ÁÐ˳ÐòºÍÊý¾ÝÀàÐÍ£ººÏ²¢µÄ±íµÄÁбØÐèƾ֤ÏàͬµÄ˳ÐòºÍÊý¾ÝÀàÐÍÅÅÁС£
NULL Öµ£ºUNION »á½« NULL ÖµÊÓΪƥÅäÖµ£¬×ÝÈ»ËüÃÇ·ºÆðÔÚ²î±ðÐеIJî±ðÁÐÖС£
ALL ºÍ DISTINCT£ºUNION ¿ÉÒÔʹÓà ALL »ò DISTINCT Òªº¦×Ö£¬ALL °üÀ¨ËùÓÐÐжø DISTINCT ½ö·µ»ØΨһÐС£
ʾÀý£º
ºÏ²¢Á½¸ö°üÀ¨¹ú¼ÒÐÅÏ¢±íµÄÐУº
SELECT Country, Population FROM Countries UNION SELECT Country, Population FROM World_Nations;
µÇ¼ºó¸´ÖÆ
Ч¹û£º
ºÏ²¢ºóµÄ±í½«°üÀ¨ËùÓйú¼ÒµÄÊý¾Ý£¬ÇÒûÓÐÖظ´ÐС£
ÒÔÉϾÍÊÇsqlÖÐunionµÄ×÷ÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡