473,396 Members | 1,797 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,396 software developers and data experts.

MySQL question- random entry

Hi,

I have a question about sql query, I want to access a random entry in
table A. It should be a simple task, what I need is to first do
"select count(*) from A" to get the total, then generate a random
number N from the total count. The problem is, now I need to access
entry no.N of table A, yet A doesn't have a counting field; so I cannot
execute something like "select * from A where id = N". Does anybody
know how to access a certain entry from its' sequential order in the
table?

Thanks a lot!
qun

Nov 7 '05 #1
4 5087
Message-ID: <11**********************@f14g2000cwb.googlegroups .com> from
Qun Cao contained the following:
Does anybody
know how to access a certain entry from its' sequential order in the
table?


SELECT * FROM table LIMIT $n,1
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Nov 7 '05 #2
thanks, Geoff.
Actually I just found this rather convenient sql command:
select * from table order by rand() limit 1;
returns one random entry from the table, exactly what i needed! :)

qun

Nov 7 '05 #3
thanks, Geoff.
Actually I just found this rather convenient sql command:
select * from table order by rand() limit 1;
returns one random entry from the table, exactly what i needed! :)

qun

Nov 7 '05 #4
Qun Cao wrote:
select * from table order by rand() limit 1;
returns one random entry from the table, exactly what i needed! :)


Be careful if your table contains a large number of rows.

Sorting by a function cannot benefit from any indexes defined on the
table. So the performance is as if you had no indexes, and for a large
table, this can cause bad performance to your PHP application.

You should try that command first and see how long it takes when applied
to your data (and also consider how much your table is expected to grow
in the future).

SELECT COUNT(*) FROM table does benefit from a unique index or primary
key on the table, but even that can be slow for a large table, or if the
index isn't cached.

You can maintain a table that notes the count of records, or you can use
SHOW TABLE STATUS to get an approximate count of records in a table.
See discussion of COUNT() in this page:
http://dev.mysql.com/doc/refman/5.0/...trictions.html

Regards,
Bill K.
Nov 8 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Westcoast Sheri | last post by:
To keep track of how many fruits my visitors buy, I use a mySQL database (2 columns: "fruit" and "quantity")....so can we make these following mySQL queries work somehow? (visitor buys 5...
0
by: MJL | last post by:
This is a mysql/php question (but a little more on the mysql side.) The two are so closely related these days, I thought it would be ok to ask here. I installed on my Suse Linux system mysql 4.0...
8
by: Tony Clarke | last post by:
Hi, Just a quick question about performance with MySQL & PHP. If I had a table in a MySQL database with about 100,000 records in it and I need to find the last record is there a quick way to do...
2
by: pieter_hordijk | last post by:
Hi all, maybe this isn't a php question, but a MySQL question. If so I'm sorry for asking you guys to help me :) I know this question is asked often in NGs, but I couldn't find the answer...
2
by: kimshapiro100 | last post by:
Question on PhP, MySQL I am thinking of a consumer internet business for which I will have to have a database driven site built. I am thinking of using PhP, MySQL as the main technologies...
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
1
by: Jim Carlock | last post by:
I have a couple questions about MySQL involving which version of MySQL to use. I'm looking for minimal memory use on a Windows XP machine. Which version would be best for this? And can anyone...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
5
by: news.telia.net | last post by:
Hi! I have a question. I have installed php and mysql on an apache-server on windows and I can't connect to the server. I tried to create a database (since I am trying to learn howto). My...
27
by: gerrymcc | last post by:
Hello, I'm a php/mysql beginner... Is there any way of making the mysql command line client full-screen? Sometimes it's easier to use the client than go thru php, but since it's only about 80...
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
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.