mysqlÖÐunionµÄÓ÷¨
ÔÚ mysql ÖУ¬union ÔËËãÇкϲ¢À´×Ô¶à¸ö±í»ò×ÓÅÌÎʵÄЧ¹û¼¯£¬Ö»·µ»ØΨһµÄЧ¹ûÐС£ËüÖ÷ÒªÓÃÓںϲ¢»¥²»ÏཻµÄЧ¹û¼¯£¬²¢ÔÊÐíͨ¹ý union all ±£´æÖظ´ÐС£
MySQL ÖÐ UNION Ó÷¨
UNION ÔËËã·ûÔÚ MySQL ÖÐÓÃÓںϲ¢À´×Ô¶à¸ö±í»ò×ÓÅÌÎʵÄЧ¹û¼¯¡£ËüÖ»·µ»ØΨһµÄЧ¹ûÐУ¬¶ø²»»áÖظ´ÏàͬµÄÐС£
Óï·¨£º
SELECT ÁÐÃû1, ÁÐÃû2, ... FROM ±íÃû1 UNION SELECT ÁÐÃû1, ÁÐÃû2, ... FROM ±íÃû2 ... [UNION ALL]
µÇ¼ºó¸´ÖÆ
²ÎÊý£º
ÁÐÃû£ºÒªºÏ²¢µÄÁÐÃû¡£ËüÃDZØÐè¾ßÓÐÏàͬµÄ˳ÐòºÍÊý¾ÝÀàÐÍ¡£
±íÃû£ºÒªºÏ²¢Ð§¹û¼¯µÄ±í»ò×ÓÅÌÎÊ¡£
UNION ALL£¨¿ÉÑ¡£©£º±£´æÖظ´ÐС£
Ó÷¨£º
UNION Ö÷ÒªÓÃÓںϲ¢À´×Ô²î±ð±í»ò×ÓÅÌÎʵĻ¥²»ÏཻµÄЧ¹û¼¯¡£ËüÖ»±£´æΨһµÄЧ¹ûÐС£
ÀýÈ磬ҪºÏ²¢±í customers ºÍ orders µÄЧ¹û¼¯£¬ÏÔʾÿ¸ö¿Í»§µÄÐÅÏ¢ºÍ¶©µ¥ÐÅÏ¢£¬¿ÉÒÔʹÓÃÒÔÏÂÅÌÎÊ£º
SELECT * FROM customers UNION SELECT * FROM orders;
µÇ¼ºó¸´ÖÆ
ÈôÊÇÒª±£´æÖظ´ÐУ¬¿ÉÒÔʹÓà UNION ALL£º
SELECT * FROM customers UNION ALL SELECT * FROM orders;
µÇ¼ºó¸´ÖÆ
×¢ÖØ£º
UNION ÔËËã·û½öºÏ²¢¾ßÓÐÏàͬÁÐÊýºÍÊý¾ÝÀàÐ͵ÄЧ¹û¼¯¡£
ÈôÊÇÁÐÃû²î±ð£¬ÐèҪʹÓÃÓÖÃû»òͨÅä·û£¨*£©À´Æ¥ÅäËüÃÇ¡£
ÈôÊÇÁоßÓвî±ðµÄÊý¾ÝÀàÐÍ£¬ÐèҪʹÓÃת»»º¯Êý£¨Èç CAST() »ò CONVERT()) À´È·±£ËüÃǼæÈÝ¡£
UNION ÔËËã·û²»Ë¼Á¿ÅÅÐò˳Ðò£¬³ý·ÇʹÓà ORDER BY ×Ӿ䡣
ÒÔÉϾÍÊÇmysqlÖÐunionµÄÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡