DESC order results

This is a discussion on DESC order results within the PHP General forums, part of the PHP Programming Forums category; Just wondering if anyone could tell me how reliable the DESC order option is going to be when I am ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-13-2008
Danny Brow
 
Posts: n/a
Default DESC order results

Just wondering if anyone could tell me how reliable the DESC order
option is going to be when I am parsing thousands of records where they
are multiple ChartNo's for the same clientNo. Or is there a better way
to grab the most recent ChartNo.

$link = mysql_connect('localhost', 'myuser', 'mypassword') or die('Could
not connect: ' . mysql());

mysql_select_db('mydatabase') or die('Could not select database');

$query = 'SELECT * FROM eChart WHERE clientNo = "2" ORDER BY ChartNo
DESC';

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

$line = mysql_fetch_array($result, MYSQL_ASSOC);


// Just for testing....
print mysql_num_rows($result);



Thanks,
Dan




--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Reply With Quote
  #2 (permalink)  
Old 01-13-2008
Jochem Maas
 
Posts: n/a
Default Re: [PHP] DESC order results

Danny Brow schreef:
> Just wondering if anyone could tell me how reliable the DESC order
> option is going to be when I am parsing thousands of records where they
> are multiple ChartNo's for the same clientNo. Or is there a better way
> to grab the most recent ChartNo.


this is a mysql question not a php question, please try to ask questions
in the appropriate forum.

the reliability of 'ORDER BY' is not tied to the ammount of
records returned.

given that you only want the 'most recent'
(by which I assume you mean the 'highest value') ChartNo for
a given clientNo you should be performing a query that returns a
single row of data.

also you shouldn't be quoting values pertaining to numeric fields
(I assume clientNo is an integer)

1. assuming ChartNo is numeric:

SELECT MAX(ChartNo) as chartno FROM eChart WHERE clientNo=2

2. assuming ChartNo is a varchar (or similar):

SELECT ChartNo FROM eChart WHERE clientNo=2 ORDER BY ChartNo DESC LIMIT 0,1

>
> $link = mysql_connect('localhost', 'myuser', 'mypassword') or die('Could
> not connect: ' . mysql());
>
> mysql_select_db('mydatabase') or die('Could not select database');
>
> $query = 'SELECT * FROM eChart WHERE clientNo = "2" ORDER BY ChartNo
> DESC';
>
> $result = mysql_query($query) or die('Query failed: ' . mysql_error());
>
> $line = mysql_fetch_array($result, MYSQL_ASSOC);
>
>
> // Just for testing....
> print mysql_num_rows($result);
>
>
>
> Thanks,
> Dan
>
>
>
>

Reply With Quote
  #3 (permalink)  
Old 01-13-2008
Danny Brow
 
Posts: n/a
Default Re: [PHP] DESC order results


Thanks for the answer, didn't think of asking this in a MySQL forum,
sorry.

Dan

On Sun, 2008-01-13 at 20:28 +0100, Jochem Maas wrote:
> Danny Brow schreef:
> > Just wondering if anyone could tell me how reliable the DESC order
> > option is going to be when I am parsing thousands of records where they
> > are multiple ChartNo's for the same clientNo. Or is there a better way
> > to grab the most recent ChartNo.

>
> this is a mysql question not a php question, please try to ask questions
> in the appropriate forum.
>
> the reliability of 'ORDER BY' is not tied to the ammount of
> records returned.
>
> given that you only want the 'most recent'
> (by which I assume you mean the 'highest value') ChartNo for
> a given clientNo you should be performing a query that returns a
> single row of data.
>
> also you shouldn't be quoting values pertaining to numeric fields
> (I assume clientNo is an integer)
>
> 1. assuming ChartNo is numeric:
>
> SELECT MAX(ChartNo) as chartno FROM eChart WHERE clientNo=2
>
> 2. assuming ChartNo is a varchar (or similar):
>
> SELECT ChartNo FROM eChart WHERE clientNo=2 ORDER BY ChartNo DESC LIMIT 0,1
>
> >
> > $link = mysql_connect('localhost', 'myuser', 'mypassword') or die('Could
> > not connect: ' . mysql());
> >
> > mysql_select_db('mydatabase') or die('Could not select database');
> >
> > $query = 'SELECT * FROM eChart WHERE clientNo = "2" ORDER BY ChartNo
> > DESC';
> >
> > $result = mysql_query($query) or die('Query failed: ' . mysql_error());
> >
> > $line = mysql_fetch_array($result, MYSQL_ASSOC);
> >
> >
> > // Just for testing....
> > print mysql_num_rows($result);
> >
> >
> >
> > Thanks,
> > Dan
> >
> >
> >
> >

>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Reply With Quote
  #4 (permalink)  
Old 01-13-2008
Larry Garfield
 
Posts: n/a
Default Re: [PHP] DESC order results

This is a MySQL question, not a PHP question. That said, what you want is the
LIMIT keyword.

On Sunday 13 January 2008, Danny Brow wrote:
> Just wondering if anyone could tell me how reliable the DESC order
> option is going to be when I am parsing thousands of records where they
> are multiple ChartNo's for the same clientNo. Or is there a better way
> to grab the most recent ChartNo.
>
> $link = mysql_connect('localhost', 'myuser', 'mypassword') or die('Could
> not connect: ' . mysql());
>
> mysql_select_db('mydatabase') or die('Could not select database');
>
> $query = 'SELECT * FROM eChart WHERE clientNo = "2" ORDER BY ChartNo
> DESC';
>
> $result = mysql_query($query) or die('Query failed: ' . mysql_error());
>
> $line = mysql_fetch_array($result, MYSQL_ASSOC);
>
>
> // Just for testing....
> print mysql_num_rows($result);
>
>
>
> Thanks,
> Dan
>
>
>
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.



--
Larry Garfield AIM: LOLG42
larry@garfieldtech.com ICQ: 6817012

"If nature has made any one thing less susceptible than all others of
exclusive property, it is the action of the thinking power called an idea,
which an individual may exclusively possess as long as he keeps it to
himself; but the moment it is divulged, it forces itself into the possession
of every one, and the receiver cannot dispossess himself of it." -- Thomas
Jefferson
Reply With Quote
  #5 (permalink)  
Old 01-13-2008
Silvio Porcellana
 
Posts: n/a
Default Re: [PHP] DESC order results

>
> $query = 'SELECT * FROM eChart WHERE clientNo = "2" ORDER BY ChartNo
> DESC';
>


If you want just one record:

$query = 'SELECT * FROM eChart WHERE clientNo = "2" ORDER BY ChartNo
DESC LIMIT 0, 1';


BTW, you'd better ask the MySQL mlist: http://lists.mysql.com/

HTH, cheers
Silvio
Reply With Quote
  #6 (permalink)  
Old 01-14-2008
Richard Lynch
 
Posts: n/a
Default Re: [PHP] DESC order results

On Sun, January 13, 2008 12:54 pm, Danny Brow wrote:
> Just wondering if anyone could tell me how reliable the DESC order
> option is going to be when I am parsing thousands of records where
> they
> are multiple ChartNo's for the same clientNo. Or is there a better way
> to grab the most recent ChartNo.


This is not actually a PHP question...

Assuming ChartNo is some kind of autoincrement field, it HAPPENS to be
100% reliable in current MySQL implementation.

Unfortunately, it's also 100% the *WRONG* way to go about this, as the
MySQL dev team could change their implementation of autoincrement at
any time, for any reason, and you'd be up the creek without a paddle[*].

If you want the most RECENT chart in time, you should time-stamp every
chart with a datetime field, and use that in DESC in your query.

You should take this discussion to the MySQL list if you wish to
understand why.
[*] It has just occured to me that being UP the creek with no paddle
isn't much of a big deal, as you can just drift back down. Being DOWN
the creek with no paddle, however, would be more problematic. English
is such a curious language...

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?
Reply With Quote
  #7 (permalink)  
Old 01-14-2008
Jochem Maas
 
Posts: n/a
Default Re: [PHP] DESC order results

Richard Lynch schreef:
> On Sun, January 13, 2008 12:54 pm, Danny Brow wrote:
>> Just wondering if anyone could tell me how reliable the DESC order
>> option is going to be when I am parsing thousands of records where
>> they
>> are multiple ChartNo's for the same clientNo. Or is there a better way
>> to grab the most recent ChartNo.

>
> This is not actually a PHP question...
>
> Assuming ChartNo is some kind of autoincrement field, it HAPPENS to be
> 100% reliable in current MySQL implementation.
>
> Unfortunately, it's also 100% the *WRONG* way to go about this, as the
> MySQL dev team could change their implementation of autoincrement at
> any time, for any reason, and you'd be up the creek without a paddle
>[*].
>
> If you want the most RECENT chart in time, you should time-stamp every
> chart with a datetime field, and use that in DESC in your query.
>
> You should take this discussion to the MySQL list if you wish to
> understand why.
>
>[*] It has just occured to me that being UP the creek with no paddle
> isn't much of a big deal, as you can just drift back down. Being DOWN
> the creek with no paddle, however, would be more problematic. English
> is such a curious language...


I think it assumes that you want to be down (where shit creek meets fresh water)
and, although the flow will take you there, shit flows so slowly that your
stuck with a decision of straving to death waiting for the drift or using you
hands as paddles ... but indeed curiouser and curiouser it is :)

>

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:41 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0