SQL UNIONµÄÓ÷¨
UNION Ö¸ÁîµÄÄ¿µÄÊǽ«Á½¸ö SQL Óï¾äµÄ½á¹ûºÏ²¢ÆðÀ´¡£´ÓÕâ¸ö½Ç¶ÈÀ´¿´£¬ UNION ¸ú JOIN ÓÐЩÐíÀàËÆ£¬ÒòΪÕâÁ½¸öÖ¸Á¿ÉÒÔÓɶà¸ö±í¸ñÖÐߢȡ×ÊÁÏ¡£ UNION µÄÒ»¸öÏÞÖÆÊÇÁ½¸ö SQL Óï¾äËù²úÉúµÄÀ¸Î»ÐèÒªÊÇͬÑùµÄ×ÊÁÏÖÖÀà¡£ÁíÍ⣬µ±ÎÒÃÇÓà UNIONÕâ¸öÖ¸Áîʱ£¬ÎÒÃÇÖ»»á¿´µ½²»Í¬µÄ×ÊÁÏÖµ (ÀàËÆ SELECT DISTINCT)¡£
UNION µÄÓï·¨ÈçÏ£º
[SQL Óï¾ä 1]
UNION
[SQL Óï¾ä 2]
¼ÙÉèÎÒÃÇÓÐÒÔϵÄÁ½¸ö±í¸ñ£¬
Store_Information ±í¸ñ
store_name
Sales
Date
Los Angeles
$1500
Jan-05-1999
San Diego
$250
Jan-07-1999
Los Angeles
$300
Jan-08-1999
Boston
$700
Jan-08-1999
Internet Sales ±í¸ñ
Date
Sales
Jan-07-1999
$250
Jan-10-1999
$535
Jan-11-1999
$320
Jan-12-1999
$750
¶øÎÒÃÇÒªÕÒ³öÀ´ËùÓÐÓÐÓªÒµ¶î (sales) µÄÈÕ×Ó¡£Òª´ïµ½Õâ¸öÄ¿µÄ£¬ÎÒÃÇÓÃÒÔÏ嵀 SQL Óï¾ä£º
SELECT Date from Store_Information
UNION
SELECT Date from Internet_Sales
½á¹û:
Date
Jan-05-1999
Jan-07-1999
Jan-08-1999
Jan-10-1999
Jan-11-1999
Jan-12-1999
ÓÐÒ»µãÖµµÃ×¢ÒâµÄÊÇ£¬Èç¹ûÎÒÃÇÔÚÈκÎÒ»¸ö SQL Óï¾ä (»òÊÇÁ½¾ä¶¼Ò»Æð) Óà "SELECT DISTINCT Date" µÄ»°£¬ÄÇÎÒÃÇ»áµÃµ½ÍêÈ«Ò»ÑùµÄ½á¹û¡£
Ïà¹ØÎĵµ£º
[Õª]ÔÚSQLÖÐɾ³ýÖظ´¼Ç¼µÄ¶àÖÖ·½·¨
±¾ÎÄ×ܽáÁËһЩɾ³ýÖظ´¼Ç¼µÄ·½·¨£¬ÔÚOracleÖУ¬¿ÉÒÔͨ¹ýΨһrowidʵÏÖɾ³ýÖظ´¼Ç¼£»»¹¿ÉÒÔ½¨ÁÙʱ±íÀ´ÊµÏÖ...ÕâÀïÖ»Ìáµ½ÆäÖеļ¸ÖÖ¼òµ¥ÊµÓõķ½·¨£¬Ï£Íû¿ÉÒԺʹó¼Ò·ÖÏí£¨ÒÔ±íemployeeΪÀý£©¡£
SQL> desc employee
Name   ......
ÄÇÎÒÃÇÈçºÎ¶Ôº¯Êý²úÉúµÄÖµÀ´É趨Ìõ¼þÄØ£¿¾ÙÀýÀ´Ëµ£¬ÎÒÃÇ¿ÉÄÜÖ»ÐèÒªÖªµÀÄÄЩµêµÄÓªÒµ¶îÓг¬¹ý $1,500¡£ÔÚÕâ¸öÇé¿öÏ£¬ÎÒÃDz»ÄÜʹÓà WHERE µÄÖ¸Áî¡£ÄÇÒªÔõô°ìÄØ£¿ºÜÐÒÔ˵أ¬SQL ÓÐÌṩһ¸ö HAVING µÄÖ¸Á¶øÎÒÃǾͿÉÒÔÓÃÕâ¸öÖ¸ÁîÀ´´ïµ½Õâ¸öÄ¿±ê¡£ HAVING ×Ó¾äͨ³£ÊÇÔÚÒ»¸ö SQL ¾ä×ÓµÄ×îºó¡£Ò»¸öº¬ÓÐ HAVING ×Ó¾äµÄ SQL ²¢²» ......
֮ǰÎÒÃÇ¿´µ½µÄ×óÁ¬½Ó (left join)£¬ÓÖ³ÆÄÚ²¿Á¬½Ó (inner join)¡£ÔÚÕâ¸öÇé¿öÏ£¬ÒªÁ½¸ö±í¸ñÄÚ¶¼ÓÐͬÑùµÄÖµ£¬ÄÇÒ»±Ê×ÊÁϲŻᱻѡ³ö¡£ÄÇÈç¹ûÎÒÃÇÏëÒªÁгöÒ»¸ö±í¸ñÖÐÿһ±ÊµÄ×ÊÁÏ£¬ÎÞÂÛËüµÄÖµÔÚÁíÒ»¸ö±í¸ñÖÐÓÐûÓгöÏÖ£¬ÄǸÃÔõô°ìÄØ£¿ÔÚÕâ¸öʱºò£¬ÎÒÃǾÍÐèÒªÓõ½ SQL OUTER JOIN (ÍⲿÁ¬½Ó) µÄÖ¸Áî¡£
ÍⲿÁ¬½ÓµÄÓï·¨ÊÇÒÀÊý¾ ......