473,385 Members | 1,730 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.

SELECTing the 'middle' row in a table (maybe with php?)

ams
A table contains records of Ice Cream flavors. There is a primary id
field (INT) and a varchar for flavor.

I need to get the row in the very middle of the table, however I can't
assume that the table's primary id field will be entirely sequential
across the table because some records may have been deleted.

Say there are 10 records in the table and the last primary id is 24.
I can't reliably devide 24 by 2 to assume the middle row is 12 because
perhaps the first 10 records were deleted.. that would mean my
"middle" row is actaully nearer to the beginning of the record set.

Is there something I can do with the total number of rows in the
table, to get the 'middle'-most row?

If I'm being too confusing ask and I'll reexplain.

Thanks!
Jul 17 '05 #1
3 3512
The Pillsbury Doughboy, speaking through ams, wrote:

Is there something I can do with the total number of rows in the
table, to get the 'middle'-most row?


offhand, the first thing that comes to mind, which may not be the most
efficient (error-checking left out for brevity):

$q = mysql_query("SELECT COUNT(*) AS total FROM ice_cream");
$rec = mysql_fetch_assoc($q);
$total = $rec['total'];
$middle = floor($total / 2);
$q = mysql_query("SELECT * FROM ice_cream LIMIT $middle,1");

note that I'm sure this works if you are ORDERing the table, but I have
no idea if an un-ORDERed SELECT will always return the rows in the same
order. I would be doubtful that it is dependable in all cases.

/joe
--
In Psi U, Rob Solomon links to the website of Rob Solomon for Scott
Watkins's network cable. Shad Hashmi memorizes the configuration of the
gi-normous memory.
Jul 17 '05 #2
On Sat, 8 Nov 2003 22:10:59 +0000 (UTC), Disco Plumber <sc**@moralminority.org>
wrote:
note that I'm sure this works if you are ORDERing the table, but I have
no idea if an un-ORDERed SELECT will always return the rows in the same
order. I would be doubtful that it is dependable in all cases.


Correct - order is not guaranteed in any way in SQL unless you have an ORDER
BY clause.

(Depends on the implementation exactly how unpredictable it is without an
ORDER BY)

A more portable way could be to just fetch all the IDs into an array, pick the
middle one, and do a select for that ID. Whether this is better or worse
depends on the volume of data.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #3
Disco Plumber wrote:
Is there something I can do with the total number of rows in the
table, to get the 'middle'-most row?


offhand, the first thing that comes to mind, which may not be the most
efficient (error-checking left out for brevity):


Does mySQL have a median statistics function ... would return the
middle value of a single valued column.

--
Spam:newsgroup(at)cr*********@verisign-sux-klj.com
EMail:<0110001100101110011000100111010101110010011 010110
11001010100000001100011011100100110000101111010011 011100
11000010111001000101110011000110110111101101101001 00000>
Jul 17 '05 #4

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

Similar topics

19
by: Ben Gribaudo | last post by:
Hello! I am trying to query a MySQL table and retrieve either the earliest or the latest message in each thread. The messages are in a table with MsgID being the primary key and fThreadID...
1
by: Philo | last post by:
How do I select all <div> tags except those which contain a <table> tag somewhere within them? Example XML: <********************** sample input ***********************> <txtSectionBody>...
5
by: luke | last post by:
I need to have two different backgrounds, one on the left and one on the right of the screen, with the main content centered in the browser. This is the code I have come up with <table...
6
by: Matik | last post by:
Hello all, I've following problem. Please forgive me not posting script, but I think it won't help anyway. I've a table, which is quite big (over 5 milions records). Now, this table contains...
4
by: Lucius | last post by:
Hello everyone, I have a query problem. I'll put it like this. There is a 'publishers' table, and there is a 'titles' table. Publishers publish titles (of course). Now I want to make a query (in...
15
by: Hi5 | last post by:
Hi, I am designing a database for a client in which It has a client table including the followings: 1-Table Client 2-Table lookupcategory 3-Table Ctegory
1
by: Jay | last post by:
Hi All, My users are complaining about the page refreshing when they are selecting multiple rows in a datagrid. Has anyone tried to manage this using javascript? I tried smartnavigation but that...
2
by: movieking81 | last post by:
If someone could help me with this, that would be great. I need to select a number of records from an SQL table based on a date range, so I started with this select. <html> <code> resultssql =...
4
by: Jeffrey Davis | last post by:
I'm hoping that someone here can give me some assistance with a database I'm trying to set up. My skills in Access are fairly basic, and I'm trying to skill up, but some of the stuff is a little...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
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
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.