.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Fetching random data from database

Posted By:ASPEvil       Posted Date: October 22, 2010    Points: 50    Category: ASP.NET    URL: http://www.dotnetspark.com  

Fetching random data from MySQL,PostgreSQL,Microsoft SQL Server,IBM DB2 and Oracle database
 

All we know how to fetch data from database and to organize them to fulfill a certain need. But there might be situations where we need to display random rows(means, the data will be from any row of particular table regardless the serial numbers). Once, I faced this situation when I was trying to build a website where a "Link Exchange" program was there, where all the links are saved in the database and I had to show only four links at a time and when the page will be refreshed, the links will be changed and will display another four links and these links may be from any row of the table which contains these links. So, I did this with SQLServer as I was working with SQLServer, but here I did mention the syntax of the query to fetch random data for MySQL,PostgreSQL,Microsoft SQL Server,IBM DB2 and Oracle database, as follow:-

Fetching random data with MySQL:

SELECT column FROM table
ORDER BY RAND()
LIMIT 1


Fetching random data with PostgreSQL:


SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1


Fetching random data with Microsoft SQL Server:


SELECT TOP 1 column FROM table
ORDER BY NEWID()


Fetching random data with IBM DB2


SELECT column, RAND() as IDX
FROM table
ORDER BY IDX FETCH FIRST 1 ROWS ONLY


Fetching random data with Oracle:


SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend