472,958 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 3494
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.