Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

ÈÃUNIONÓëORDER BY²¢´æÓÚSQLÓï¾äµ±ÖÐ

http://www.cnblogs.com/yinzhenzhixin/archive/2009/01/07/1371064.html
ÔÚSQLÓï¾äÖУ¬UNION¹Ø¼ü×Ö¶àÓÃÀ´½«²¢ÁеĶà×é²éѯ½á¹û(±í)ºÏ²¢³ÉÒ»¸ö½á¹û(±í)£¬¼òµ¥ÊµÀýÈçÏ£º
SELECT [Id],[Name],[Comment] from [Product1]
UNION
SELECT [Id],[Name],[Comment] from [Product2]
ÉÏÃæµÄ´úÂë¿ÉÒÔʵÏÖ½«´ÓProduct1ºÍProduct2Á½ÕűíºÏ²¢³ÉÒ»¸ö±í£¬Èç¹ûÄúÖ»ÊÇÏ£ÍûºÏ²¢Á½ÕűíÖзûºÏÌØ¶¨Ìõ¼þµÄ¼Ç¼ÒÖ»òÊǺϲ¢Á½Õűí¸÷×ÔµÄǰNÌõ¼Ç¼£¬ÄÇôÄúµÄ´úÂë¿ÉÄÜ»áÏñÏÂÃæÕâÑùд£º
SELECT [Id],[Name],[Comment] from [Product1] WHERE LEN([Name]) > 5
UNION
SELECT [Id],[Name],[Comment] from [Product2] WHERE [Id] IN (11,20) AND [Comment] IS NOT NULL
SELECT TOP N [Id],[Name],[Comment] from [Product1]
UNION
SELECT TOP N [Id],[Name],[Comment] from [Product2]
This is so easy£¡µ«ÊǼÙÈçÄúÏ£Íû´Ó°üº¬Type×ֶεÄij±íÖиù¾ÝType·Ö±ðËæ»úɸѡNÌõ¼Ç¼²¢½«½á¹ûºÏ²¢³ÉÒ»ÕÅ±í£¬Äú¿ÉÄÜ»áÏñÏÂÃæÕâÑùд£º
SELECT TOP N [Id],[Name],[Comment] from [Product] WHERE [Type]='TYPE1' ORDER BY NEWID()
UNION
SELECT TOP N [Id],[Name],[Comment] from [Product] WHERE [Type]='TYPE2' ORDER BY NEWID()
UNION
SELECT TOP N [Id],[Name],[Comment] from [Product] WHERE [Type]='TYPE3' ORDER BY NEWID()
UNION
SELECT TOP N [Id],[Name],[Comment] from [Product] WHERE [Type]='TYPE4' ORDER BY NEWID()
UNION
SELECT TOP N [Id],[Name],[Comment] from [Product] WHERE [Type]='TYPE5' ORDER BY NEWID()
UNION
SELECT TOP N [Id],[Name],[Comment] from [Product] WHERE [Type]='TYPE6' ORDER BY NEWID()
UNION
SELECT TOP N [Id],[Name],[Comment] from [Product] WHERE [Type]='TYPE7' ORDER BY NEW


Ïà¹ØÎĵµ£º

¹ØÓÚ³ÌÐò´úÂëÖеÄSQLÓï¾ä

ÔÚ³ÌÐòÖÐÓÐЩ²éѯÓï¾äÏà¶Ô½Ï³¤£¬¿ÉÒÔ½«Óï¾äµ¥¶ÀдÔÚÒ»¸öXXX.sqlÎļþÖУ¬ÔÚ³ÌÐòÖжÁÈ¡SQLÎļþ
¾ßÌåÉæ¼°µ½
import java.io.File;
import org.apache.commons.io.FileUtils;
import java.net.URL;
URL resourceUrl = XXXX.class.getClassLoader().getResource(SQL_PATH+sqlName);//SQL_PATH¾ßÌåSQLÎļþ´æÔÚ·¾¶£¬sqlName¼ ......

ÍÆ¼ö̽ÌÖÕ䲨¡¾¾­µäSQLÓï¾ä´óÈ«¡¿

ÏÂÁÐÓï¾ä²¿·ÖÊÇMssqlÓï¾ä£¬²»¿ÉÒÔÔÚaccessÖÐʹÓá£
¡¡¡¡SQL·ÖÀࣺ
¡¡¡¡DDL—Êý¾Ý¶¨ÒåÓïÑÔ(CREATE£¬ALTER£¬DROP£¬DECLARE)
¡¡¡¡DML—Êý¾Ý²Ù×ÝÓïÑÔ(SELECT£¬DELETE£¬UPDATE£¬INSERT)
¡¡¡¡DCL—Êý¾Ý¿ØÖÆÓïÑÔ(GRANT£¬REVOKE£¬COMMIT£¬ROLLBACK)
¡¡¡¡Ê×ÏÈ,¼òÒª½éÉÜ»ù´¡Óï¾ä£º
¡¡¡¡1¡¢ËµÃ÷£º´´½¨Êý¾Ý¿â
......

ÌùÉÏÊղصÄSQL³£Ó÷ÖÒ³µÄ°ì·¨¡«¡«

Ó¦Ò»¸öÅóÓѵÄÒªÇó£¬ÌùÉÏÊղصÄSQL³£Ó÷ÖÒ³µÄ°ì·¨¡«¡«
±íÖÐÖ÷¼ü±ØÐëΪ±êʶÁУ¬[ID] int IDENTITY (1,1)
1.·ÖÒ³·½°¸Ò»£º(ÀûÓÃNot InºÍSELECT TOP·ÖÒ³)
Óï¾äÐÎʽ£º 
SELECT TOP Ò³¼Ç¼ÊýÁ¿ *
from ±íÃû
WHERE (ID NOT IN
  (SELECT TOP (ÿҳÐÐÊý*(Ò³Êý-1)) ID
  from ±íÃû
  ORDER BY ......

SQL֪ʶÕûÀí

Ò»£ºSelectÓï¾ä£º
       select ×Ö¶ÎÃû from ±íÃû where Ìõ¼þ order by ÅÅÐò
see:select distinct ´Ó¶à¸öÏàͬ×Ö¶ÎÖÐץΨһֵ    
see:²éÕÒld_det_andy±íÖÐÓÐÊý¾Ýµ«ld_det_temp±íÖÐûÊý¾ÝµÄÊý¾Ý
        select * from ld_det_a ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ