×ðÁú¿­Ê±¹ÙÍøµÇ¼

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µÄÓ÷¨µÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ