473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Getting random row from MySQL

144 100+
Hi, i'm learning php and MySQL

and i want to display only let's say 4 records (different/random every page load) from my query that could be more than 4. but no repeating records would be shown.

i could think a way that's kinda hard. is there any function in php or mysql that can make it easier?

thx
Dec 26 '07 #1
2 1214
Atli
5,058 Expert 4TB
Hi.

A very simple way to do this would be to simply use a query like:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tbl ORDER BY RAND() LIMIT 4
  2.  
But this will get slow on large tables.

Another way would be to find the highest possible ID in your table, select a few random numbers and fetch only those rows.

Keep in mind that this will not be truly random if there are gaps in the ID's in your table, but I doubt that will have any notable effect.
Dec 26 '07 #2
thesti
144 100+
hi, thx Atli

i'll try this one.

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tbl ORDER BY RAND() LIMIT 4
  2.  
Dec 26 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Rahul Anand | last post by:
Hi all, I am trying to fetch 5 records, randomly picked and in random order from a MySQL table (MySQL version > 3.23). I wrote my SQL Query as SELECT name,id FROM tablename WHERE active =...
0
by: Jason McCormick | last post by:
Hello all, I was wondering if someone could shed some light on this problem I'm having. I have 3 slave MySQL servers replication from a master machine. The master machine is running only MySQL...
22
by: James Kupernik | last post by:
Hello everyone! I'm hoping someone can shed some light on my ever daunting mysql/php problem. I'm currently trying to get a new server up and running using apache/php/mysql. Everything runs...
3
by: VB.NET | last post by:
I'm using a mysql database and connecting my vb.net program to the DB over a network connection. i would like to bring this data over to a vb.net random access file. does anyone know how to...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
4
by: Yobbo | last post by:
Hi All My query is as follows: SELECT STRINGTEXT, TOKENID FROM WEBSTRINGS WHERE TOKENID IN (6,20,234,19,32,4,800,177) All I want is my resultset to come back in the order that I have...
3
by: tshad | last post by:
I have a page that I am getting a username and password as a random number (2 letters, one number and 4 more letters) I have 2 functions I call: *************************************************...
2
by: federicog | last post by:
I have to create a listing in random order from a few mysql tables. I can't use ORDER BY RAND() for a few reasons: - The random order has to change only every 24hs, ie: today it will show rows...
2
by: Cosmic programmer | last post by:
I need to generate a random value from x to y having increment i. where x is start value, y is end value. so if x = 2 and y = 12 and i = 2 then random value should be generated from the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.