N randomly selected records of SQL

  N randomly selected records of SQL (mysql and sqlserver) 

  Mysql: select * from tablename order by rand () limit 10 
  Sqlserver: select top 10 * from tablename order by NEWID () 

  Subject to the conditions in the data, from the start by five after 10 data 
  Mysql: select * from table limit 5,10 
  Sqlserver: * SELECT TOP 10 
  FROM table WHERE (IDENTITYCOL NOT IN 
  (SELECT TOP 5 IDENTITYCOL 
  FROM table order by IDENTITYCOL)) 
  Order by IDENTITYCOL 

  Or: 
  Select * IDENTITY (int, 1,1) as Num into aa from tablename # # 
  Select * from aa where Num # #> = 5 and Num <= 10 

  Note: 

IDENTITYCOL
  Back to marking out.    For more information, please see IDENTITY (attributes), the CREATE TABLE and ALTER TABLE. 
  If the FROM clause in the table a number of attributes that include IDENTITY column, it must be specified by the table (such as T1.IDENTITYCOL) IDENTITYCOL limit. 

ROWGUIDCOL
  Returns global unique identifier out. 
  If the FROM clause in the table with a number of ROWGUIDCOL attribute, it must be specified by the table (such as T1.ROWGUIDCOL) ROWGUIDCOL limit. 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Tags:

Releated Articles


0 Comments to “N randomly selected records of SQL”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.