Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö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Óï¾ä? ORACLE SQLSERVER ECT.

MySQL:
SELECT column from table
ORDER BY RAND()
LIMIT 1
PostgreSQL:
SELECT column from table
ORDER BY RANDOM()
LIMIT 1
Microsoft SQL Server:
SELECT TOP 1 column from table
ORDER BY NEWID()
IBM DB2
SELECT column, RAND() as IDX
from table
ORDER BY IDX FETCH FIRST 1 ROWS ONLY
Thanks Ti ......

һЩ»ù±¾µÄSQLÃüÁî

--ÏÔʾ°æ±¾ºÅ£¬µ±Ç°ÈÕÆÚ
SELECT VERSION(),CURRENT_DATE(),NOW();
--Ãâ·ÑµÄ¼ÆËãÆ÷
SELECT (20+5)*4 AS RESULT,SIN(PI()/3);
--´´½¨Êý¾Ý¿â
CREATE DATABASE databasename;
--ɾ³ýÊý¾Ý¿â
DROP DATABASE databasename;
--ÏÔʾµ±Ç°´æÔÚµÄÊý¾Ý¿â
SHOW DATABASES;
--Ñ¡ÔñÊý¾Ý¿â
USE ......

SQL Server µÃµ½ÐкŵÄSQL

SQL Server µÃµ½ÐкŵÄSQL
ʹÓÃÁÙʱ±í£º
select   id=identity(int,1,1),value   into   #temp   from   YourTable  
  select   *   from   #temp  
  drop   table   #temp  
È¡µÃµÚ11µ½20ÐмǼ£º
select   IDENTITY(in ......

SQL֪ʶÕûÀí

Ò»£ºSelectÓï¾ä£º
       select ×Ö¶ÎÃû from ±íÃû where Ìõ¼þ order by ÅÅÐò
see:select distinct ´Ó¶à¸öÏàͬ×Ö¶ÎÖÐץΨһֵ    
see:²éÕÒld_det_andy±íÖÐÓÐÊý¾Ýµ«ld_det_temp±íÖÐûÊý¾ÝµÄÊý¾Ý
        select * from ld_det_a ......

DB2 SQL PL½éÉÜ


DB2 SQL PL
SQL PLÊÇDB2ËùÖ§³ÖµÄ¹ý³Ì»¯ÓïÑÔ£¬ËüÊÇSQL/PSM±ê×¼µÄÒ»¸ö×Ó¼¯¡£Æä¸ù¾ÝÓ¦Ó÷¶Î§²»Í¬£¬ÓÖ·ÖΪInline SQL PL£¬Embeded SQL PLºÍCompiled SQL PL¡£
Inline SQL PL
ÊÊÓ÷¶Î§£º´¥·¢Æ÷¡¢º¯ÊýºÍ·½·¨£¬Ö§³Ö²¿·ÖSQL PL£¬Ê¹ÓÃʱҪעÒâһЩÏÞÖÆ
Óï·¨¹æÔò£ºBEGIN ATOMIC ... END
Embeded SQL PL
ÊÊÓ÷¶Î§£ºÇ¶Èëʽ£¬Åäº ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ