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

ÈýÖÖ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 ID))
  ORDER BY ID
//×Ô¼º»¹¿ÉÒÔ¼ÓÉÏһЩ²éѯÌõ¼þ


Àý:
select top 2 *
from Sys_Material_Type
where (MT_ID not in
    (select top (2*(3-1)) MT_ID from Sys_Material_Type  order by MT_ID))
order by MT_ID



2.·ÖÒ³·½°¸¶þ£º(ÀûÓÃID´óÓÚ¶àÉÙºÍSELECT TOP·ÖÒ³£©

Óï¾äÐÎʽ£º
SELECT TOP ÿҳ¼Ç¼ÊýÁ¿ *
from ±íÃû
WHERE (ID >
          (SELECT MAX(id)
    from (SELECT TOP ÿҳÐÐÊý*Ò³Êý id  from ±í
          ORDER BY id) AS T)
      )
ORDER BY ID

Àý:
SELECT TOP 2 *
from Sys_Material_Type
WHERE (MT_ID >
          (SELECT MAX(MT_ID)
          from (SELECT TOP (2*(3-1)) MT_ID
                from Sys_Material_Type
                ORDER BY MT_ID) AS T))
ORDER BY MT_ID



3.·ÖÒ³·½°¸Èý£º(ÀûÓÃSQLµÄÓαê´æ´¢¹ý³Ì·ÖÒ³)
create  procedure SqlPager
@sqlstr nvarchar(4000), --²éѯ×Ö·û´®
@currentpage int, --µÚNÒ³
@pagesize int --ÿҳÐÐÊý
as
set nocount on
declare @P1 int, --P1ÊÇÓαêµÄid
@rowcount int
exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1, @rowcount=@rowcount output
select ceiling(1.0*@rowcount/@pagesize) as ×ÜÒ³Êý--,@rowcount as ×ÜÐÐÊý,@currentpage as µ±Ç°Ò³
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @P1,16,@currentpage,@pagesize
exec sp_cursorclose @P1
set nocount off



4.×ܽá:
ÆäËüµÄ·½°¸£ºÈç¹ûûÓÐÖ÷¼ü£¬¿ÉÒÔÓÃÁÙʱ±í£¬


Ïà¹ØÎĵµ£º

SQLÖÐÈçºÎÓÃÒ»¸ö±í¸üÐÂÁíÒ»¸ö±í

for ACCESS :
update a, b set a.name=b.name1 where a.id=b.id
for SQL Server:
"update a set a.name=b.name1 from a,b where a.id=b.id"
update   a   set   a.status=b.status  
  from   table1   a,table2   b  
&nbs ......

SQL Server 2005ÖÐÐÂÔöµÄ¹¦ÄÜÇ¿´óµÄ´°¿Úº¯Êý

Ô­ÎĵØÖ·£ºhttp://www.cnblogs.com/changhai0605/articles/1276319.html
OracleµÄÇë²Î¿¼£ºhttp://zonghl8006.blog.163.com/blog/static/4528311520083995931317/
1.¼ò½é£º
SQL Server 2005ÖÐÐÂÔöµÄ´°¿Úº¯Êý°ïÖúÄãѸËٲ鿴²»Í¬¼¶±ðµÄ¾ÛºÏ£¬Í¨¹ýËü¿ÉÒԷdz£·½±ãµØÀÛ¼Æ×ÜÊý¡¢Òƶ¯Æ½¾ùÖµ¡¢ÒÔ¼°Ö´ÐÐÆäËü¼ÆËã¡£
´°¿Úº¯Êý¹¦ÄÜ·Ç ......

±ðÈöÔSql Server µÄÐÔÄܲâÊÔÊý¾ÝÃÔ»óÄã

Êý¾Ý¿âµÄÐÔÄܲâÊÔ¿ÉÒÔ°ïÖúÄãÌáÇ°ÖªµÀÄãµÄϵͳµÄ¸ºÔØÄÜÁ¦£¬¿ÉÒÔ°ïÖúÄã¸Ä½øϵͳµÄʵʩ»òÉè¼Æ£¬¿ÉÒÔ°ïÖúÄãÈ·¶¨Ò»Ð©Éè¼ÆºÍ±à³ÌÔ­Ôò. µ«ÊÇ£¬ÕâÀïÃæÒ²ÓÐÏÝÚå. Èç¹û²»Ð¡ÐÄ£¬Äã»á×Ô¼º°Ñ×Ô¼ºÏݽøÈ¥£¬È´×îÖÕ²»Ã÷°×ÊÇʲôԭÒò. ÕâÀÎÒÄÃһλÏÈÉúΪÀý£¬À´¿´¿´ËûÔõô×Ô¼º°Ñ×Ô¼º¸ãºýÍ¿µÄ.
×î½ü, ÏëÆðÔÚ´æ´¢¹ý³ÌÖо¿¾¹ÊÇʹÓÃÁÙʱ±í»¹ÊÇÊ ......

Ëæ»úÑ¡ÔñÐеÄ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 ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ