SQL UnionÓ÷¨×ܽá
UNION ÔËËã·û½«¶à¸ö SELECT Óï¾äµÄ½á¹û×éºÏ³ÉÒ»¸ö½á¹û¼¯¡£ £¨£±£©Ê¹Óà UNION ÐëÂú×ãÒÔÏÂÌõ¼þ£º £Á£ºËùÓвéѯÖбØÐë¾ßÓÐÏàͬµÄ½á¹¹£¨¼´²éѯÖеĵÄÁÐÊýºÍÁеÄ˳Ðò±ØÐëÏàͬ£©¡£ £Â£º¶ÔÓ¦ÁеÄÊý¾ÝÀàÐÍ¿ÉÒÔ²»Í¬µ«ÊDZØÐë¼æÈÝ£¨ËùνµÄ¼æÈÝÊÇÖ¸Á½ÖÖÀàÐÍÖ®¼ä¿ÉÒÔ½øÐÐÒþʽת»»£¬²»ÄܽøÐÐÒþʽת»»Ôò±¨´í£©¡£Ò²¿ÉÒÔÓÃÏÔʽת»»ÎªÏàͬµÄÊý¾ÝÀàÐÍ¡£ £¨µ±Êý¾ÝÀàÐͲ»Í¬Ê±£¬Ôò¸ù¾ÝÊý¾ÝÀàÐÍÓÅÏȼ¶¹æÔòÈ·¶¨Ëù²úÉúµÄÊý¾ÝÀàÐÍ¡£Èç¹ûÀàÐÍÏàͬ£¬µ«¾«¶È¡¢Ð¡ÊýλÊý»ò³¤¶È²»Í¬£¬Ôò¸ù¾ÝÓÃÓںϲ¢±í´ïʽµÄÏàͬ¹æÔòÀ´È·¶¨½á¹û£©£¨²ÎÕգͣӣģΣºÊý¾ÝÀàÐÍÓÅÏȼ¶£© £Ã£ºÈç¹ûΪ£Ø£Í£ÌÊý¾ÝÀàÐÍÔòÁбØÐëµÈ¼Û£¨ËùÓÐÁж¼±ØÐëÀàÐÍ»¯Îª XML ¼Ü¹¹£¬»òÕßΪ·ÇÀàÐÍ»¯£©£¬Èç¹ûÀàÐÍ»¯£¬ÕâЩÁбØÐëÀàÐÍ»¯ÎªÏàͬµÄ XML ¼Ü¹¹¼¯ºÏ¡£ £¨£²£©UNION½á¹û¼¯ £Á£ºUNION ºóµÄ½á¹û¼¯µÄÁÐÃûÓë µÚÒ»¸ö SELECT Óï¾äµÄ½á¹û¼¯ÖеÄÁÐÃûÏàͬ¡£ÁíÒ»¸ö SELECT Óï¾äµÄ½á¹û¼¯ÁÐÃû½«±»ºöÂÔ¡£ £Â£º£Õ£Î£É£Ï£ÎĬÈϵÄɾ³ý½á¹û¼¯ÖÐÖظ´µÄ¼Ç¼£¬Èç¹ûʹÓà £Á£Ì£Ì ¹Ø¼ü×ÖÔòÔÚ½á¹û¼¯Öб£ÁôËùÓмǼ£¨±£ÁôÖظ´¼Ç¼£© £¨£³£©×¢Òâµã £Á£º £Ôransact-SQL Óï¾äÖпÉÒÔʹÓöà¸ö UNION ÔËËã·û £Â£ºÔÚʹÓÃÁË UNION ÔËËã·ûµÄ¸÷¸ö SELECT Óï¾ä²»ÄÜ°üº¬ËüÃÇ×Ô¼ºµÄ ORDER BY »ò COMPUTE ×Ӿ䡣¶øÖ»ÄÜÔÚ×îÖÕµÄ×éºÏ½á¹û¼¯£¨¼´×îºóÒ»¸ö SELECT Óï¾äµÄºóÃ棩ʹÓÃÒ»¸ö ORDER BY »ò COMPUTE ×Ӿ䡣 £Ã£ºÔÚʹÓÃÁË UNION ÔËËã·ûµÄ¸÷¸ö SELECT Óï¾äÖпÉÒÔʹÓà GROUP BY ºÍ HAVING ×Ӿ䡣 £Ä£ºÄ¬ÈÏÇé¿öÏ£¬SQL Server 2005 ´Ó×óÏòÓÒ¶Ô°üº¬ UNION ÔËËã·ûµÄÓï¾äÇóÖµ¡£¿ÉʹÓÃÔ²À¨ºÅÖ¸¶¨Çóֵ˳Ðò¡£ £Í£Ó£Ä£Î Àý×ÓÈçÏ ÀýÈ磬ÒÔÏÂÓï¾ä²¢²»µÈ¼Û£º /* First statement. */
SELECT * from TableA
UNION ALL
( SELECT * from TableB
UNION
SELECT * from TableC
)
GO
/* Second statement. */
(SELECT * from TableA
UNION ALL
SELECT * from TableB
)
UNION
SELECT * from TableC)
GO ÔÚµÚÒ»¸öÓï¾äÖУ¬½«Ïû³ý TableB Óë TableC µÄ²¢¼¯ÖеÄÖظ´ÐС£¶øÔڸü¯Óë TableA µÄ²¢¼¯ÖУ¬½«²»Ïû³ýÖظ´ÐС£ÔÚµÚ¶þ¸öÓï¾äÖУ¬TableA ÓëTableB µÄ²¢¼¯Öн«°üº¬Öظ´ÐУ¬µ«ÔÚËæºóÓë TableC µÄ²¢¼¯Öн«Ïû³ýÖظ´ÐС£ALL ¹Ø¼ü×ֶԴ˱í´ïʽµÄ×îÖÕ½á¹ûûÓÐÓ°Ïì¡£ £¨£´£©Àý×Ó ²ÎÕգͣӣģÎ
Ïà¹ØÎĵµ£º
Ò»¡¢Ñ»·
create table tb(
col1 varchar(1),
col2 varchar(2)
)
insert tb(col1,col2)values('0','0')
go 10000000
¶þ¡¢Êý¾ÝºÏ²¢
if object_id('[order]') is not null drop table [order]
go
create table [order]([orderid] int,[ordertype] varchar(1))
insert [ord ......
ʹÓÃSQLʱ±ØÐ뿼ÂǵĹؼüÒòËØ
Ò»£¬»ñµÃ½á¹û¼¯ËùÐè·ÃÎʵÄÊý¾ÝÁ¿£¬ÔÚûÓÐÈ·¶¨Ä¿±êÈÝÁ¿Ö®Ç°£¬ºÜÄѶ϶¨²éѯִÐеÄЧÂÊ£»
¶þ£¬¶¨Òå½á¹û¼¯ËùÐèµÄ²éѯÌõ¼þ£¬Ò²¾ÍÊÇÈçºÎÏÞ¶¨½á¹û¼¯£¬ÈçºÎºÏÀíµÄʹÓÃsql×Ӿ䣻
Èý£¬½á¹û¼¯µÄ´óС£¬È¡¾öÓÚ±íµÄ´óСºÍ¹ýÂËÌõ¼þµÄϸ½Ú£¬µ«²»¶¼ÊÇÕâÑù£¬µäÐ͵ÄÇé¿öÊÇ£¬Èô¸É¸ö¶ÀÁ¢Ê¹ÓÃʱЧÂʲ»¸ßµÄÌõ ......
·Ï»°ÉÙ˵,(ÒѾÓоä·Ï»°ÁË.)
¿´±í
Óï¾ä: SELECT * from C
//---------------------------------------
Cno Cname Teacher
---- ---------- --------
1 ÓïÎÄ Æî¾²
2 &n ......
1) ʱ¼äת»»º¯ÊýÖÐÈç¹ûÓÐʱ¼ä±äÁ¿yyyy-mm-ddÁ½±ßÐèÁ½''
to_date(''' || to_char(a_valid_date_end, 'yyyy-mm-dd') ||''', ''yyyy-mm-dd'')
2) select distinct(e.itemnum) bulk collect into v_itemnum
½«×Ö·û´®Êý×éÒÔ·Ö¸ô·û·Ö¸ôµÄ×Ö·û´®µ÷ÓÃ
pkg_maximo_common.get_string(v_itemnum, ',', '')
3) unionÖ»Êǽ«Á½¸ö½á¹ ......
GROUP BY ×Ó¾äÓÃÀ´¶ÔÖ¸¶¨µÄ×Ö¶Î×ö·Ö×飬²úÉúÒ»¸ö»ã×ÜÐÅÏ¢¡£ £¨1£©group byÓï¾ä¶ÔselectºóËùÑ¡ÔñµÄ×Ö¶ÎÓÐÒ»¶¨µÄÏÞÖÆ£¬¼´selectºóûÓÐʹÓþۺϺ¯ÊýµÄ×ֶαØÐë°üº¬ÔÚgroup by Óï¾äºóÃæµÄ½á¹û¼¯ÖС££¨GROUP BY ¹Ø¼ü×Öºó¸úÒ»¸öÁеÄÁÐ±í£¬³ÆΪ×éºÏÁУ©. £¨2£©²»ÄܶÔÊý¾ÝÀàÐÍΪ ntext¡¢text¡¢image »ò bit µÄÁÐʹÓà GROUP BY »ò ......