mysqlÖеÄunionºÍunion allµÄÇø±ð
mysqlÖеÄunionºÍunion allÜöÝͲÙ×÷·ûÖ®¼äµÄ²î±ð£ºunion·µ»ØΨһÐУ¬¶øunion all·µ»ØËùÓÐÐУ¬°üÀ¨Öظ´ÐС£union°´ÉýÐò¶ÔЧ¹û¼¯ÅÅÐò£¬¶øunion all²»ÅÅÐò¡£Ñ¡ÔñunionÏû³ýÖظ´ÐУ¬¶øÑ¡Ôñunion all±£´æÖظ´ÐС£
UNION Óë UNION ALL£ºMySQL ÖÐÁ½¸öÜöÝͲÙ×÷·ûµÄ²î±ð
ÔÚ MySQL ÖУ¬UNION ºÍ UNION ALL ÊÇÓÃÓÚ×éºÏÁ½¸ö»ò¶à¸ö±íÖÐÊý¾ÝµÄÇ¿Ê¢²Ù×÷·û¡£È»¶ø£¬ËüÃÇÖ®¼ä±£´æ×ÅÖ÷ÒªµÄ²î±ð¡£
UNION
½ö·µ»ØЧ¹û¼¯ÖÐΨһµÄÐС£
ÔÚ½«ÐÐÌí¼Óµ½Ð§¹û¼¯Ö®Ç°£¬»á°´ÉýÐò¶ÔÐоÙÐÐÅÅÐò¡£
Ïû³ýÖظ´µÄÐУ¬×ÝÈ»ËüÃÇÀ´×Ô²î±ðµÄ±í¡£
UNION ALL
·µ»ØЧ¹û¼¯ÖÐËùÓÐÐУ¬°üÀ¨Öظ´ÐС£
²»»á¶ÔÐоÙÐÐÅÅÐò¡£
±£´æÖظ´ÐУ¬×ÝÈ»ËüÃÇÀ´×Ô²î±ðµÄ±í¡£
Ñ¡ÔñʹÓÃÄÄÖÖ²Ù×÷·û
Ñ¡Ôñ UNION »ò UNION ALL È¡¾öÓÚÌض¨ÐèÇó£º
ÈôÊÇÐèÒªÏû³ýÖظ´ÐУ¬ÇëʹÓà UNION¡£
ÈôÊÇÐèÒª±£´æËùÓÐÐУ¬°üÀ¨Öظ´ÐУ¬ÇëʹÓà UNION ALL¡£
ÈôÊÇÐèÒª¶ÔЧ¹û¾ÙÐÐÅÅÐò£¬ÇëʹÓà UNION¡£
ÈôÊÇÅÅÐòÎ޹ؽôÒª£¬ÇëʹÓà UNION ALL¡£
ʾÀý
˼Á¿ÒÔÏÂÁ½¸ö±í£º
±í A£º
Student_ID | Student_Name |
---|---|
1 | John |
3 | Mary |
±í B£º
Student_ID | Student_Name |
---|---|
3 | Mary |
5 | Bob |
ʹÓà UNION ²Ù×÷·û£º
SELECT * FROM A UNION SELECT * FROM B;
µÇ¼ºó¸´ÖÆ
Ч¹û£º
| Student_ID | Student_Name | |---|---| | 1 | John | | 3 | Mary | | 5 | Bob |
µÇ¼ºó¸´ÖÆ
ʹÓà UNION ALL ²Ù×÷·û£º
SELECT * FROM A UNION ALL SELECT * FROM B;
µÇ¼ºó¸´ÖÆ
Ч¹û£º
| Student_ID | Student_Name | |---|---| | 1 | John | | 3 | Mary | | 3 | Mary | | 5 | Bob |
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇmysqlÖеÄunionºÍunion allµÄÇø±ðµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡