Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar Mark Forums Read
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 10-19-2004, 03:58 PM   #1
Mortimer
Confirmed User
 
Join Date: Oct 2002
Location: Where the hell am I now?
Posts: 153
MySQL sorting issue question

Hi,

does anybody know how to sort the rows in order of the last inserted row to the first inserted row without using a specific column for that? When you make a select in MySQL and do not specify any SORT BY clause, it sorts the results in the order they were inserted in the database. Is it possible to get the exact opposite of that?

Thanks for your help!
__________________
The wiseman owns little but knows much, while the fool knows little but owns much
Mortimer is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-19-2004, 04:22 PM   #2
M_M
Confirmed User
 
Join Date: May 2004
Posts: 1,167
Create a temporary table, the same structure, except add an autoincrement field

Select from original insert to the temporary table, than select from temporary table order by the autoincrement field descending order.
__________________
;-)!;-)!;-)!;-)!;-)!;-)!;-)!;-)!;-)!;-)!;-)
M_M is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-19-2004, 04:29 PM   #3
fuzebox
making it rain
 
fuzebox's Avatar
 
Industry Role:
Join Date: Oct 2003
Location: seattle
Posts: 22,119
Every table should have a unique id field.
fuzebox is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-19-2004, 04:31 PM   #4
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,115
Without using a unique id somehere in the query, it would be hard to get it using the order clause, however,

Why not just read through your results set backwards:

........

$sql_str="select * from table_i_want_to_read";
$result = mysql_query($sql_str,$db);

//Reading through frontwards

for ($i = 0; $i < mysql_num_rows($result); $i++ )

{

echo "field: ". mysql_result($result,$i,"field" ) ;

}

//reverse the read

for ($i = mysql_num_rows($result); $i>=0; $i-- )

{

echo "field:: ". mysql_result($result,$i,"field" ) ;

}

..........

edited in. The brackets seem to be being replaced with haha123 or some such
__________________
All cookies cleared!

Last edited by sarettah; 10-19-2004 at 04:35 PM..
sarettah is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-19-2004, 05:15 PM   #5
bawdy
Confirmed User
 
Join Date: Feb 2002
Posts: 1,424
non of these methods are fully accurate... the order that records are returned to a record set are not necessarily in the order that there were inserted... nor are you guaranteed to get the record set back in the same order on any subsequent query
bawdy is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-19-2004, 05:30 PM   #6
JSA Matt
So Fucking Banned
 
Join Date: Aug 2003
Location: San Diego, CA
Posts: 5,464
Why on earth would you waste all this time trying to find a way around something when you can simply add a field with a unique ID and use the SORT?

Quote:
Originally posted by bawdy
non of these methods are fully accurate... the order that records are returned to a record set are not necessarily in the order that there were inserted... nor are you guaranteed to get the record set back in the same order on any subsequent query
They're not? So what is a fully accurate method?
JSA Matt is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-19-2004, 06:25 PM   #7
Paul Waters
Confirmed User
 
Paul Waters's Avatar
 
Join Date: Mar 2003
Location: Toronto, Ontario
Posts: 4,402
Create a field for a timestamp.

Use it.
__________________


Paul
Paul Waters is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-19-2004, 08:05 PM   #8
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,115
Quote:
Originally posted by bawdy
non of these methods are fully accurate... the order that records are returned to a record set are not necessarily in the order that there were inserted... nor are you guaranteed to get the record set back in the same order on any subsequent query
Actually, with MYSQL, because it is ISAM, you should by default get back the native order, which is the order the records were added to the table.

With an ISAM table it is a sure thing because the table is built with a default key pointing at the actual record number, with other database structures it is not.
__________________
All cookies cleared!
sarettah is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-19-2004, 08:06 PM   #9
malakajoe
Confirmed User
 
Join Date: Feb 2003
Location: Sacramento
Posts: 1,751
Quote:
Originally posted by Paul Waters
Create a field for a timestamp.

Use it.
__________________
Selfpleasure.com for sale on auction. Closes on Tuesday March 11th at 9pm PST!!!!

Dirty enough to be good, but clean enough for everyone!
------------------------------------------------------------------

Moral Police - First graduating class coming soon! - Forcing our values across the internet
malakajoe is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-19-2004, 08:09 PM   #10
cezam
Confirmed User
 
Join Date: Jun 2003
Location: CZ, EU
Posts: 1,363
Quote:
Originally posted by JSA Matt
Why on earth would you waste all this time trying to find a way around something when you can simply add a field with a unique ID and use the SORT?
Exactly. Just add an auto_increment primary key, and use it to sort the table.
cezam is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-19-2004, 08:13 PM   #11
Lane
Will code for food...
 
Join Date: Apr 2001
Location: Buckeye, AZ
Posts: 8,496
Rows are not always appended to the end of the table.
If there has been any deleted rows, new rows might be inserted into their place, instead of the end of the table.


Best practice is, either use a primary key with autoincrement or a timestamp field which will keep the insert/update time for each row.
__________________
Lane is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-19-2004, 08:16 PM   #12
JSA Matt
So Fucking Banned
 
Join Date: Aug 2003
Location: San Diego, CA
Posts: 5,464
Quote:
Originally posted by Lane
Rows are not always appended to the end of the table.
If there has been any deleted rows, new rows might be inserted into their place, instead of the end of the table.


Best practice is, either use a primary key with autoincrement or a timestamp field which will keep the insert/update time for each row.
I thought they only did that when the primary key was already present?
JSA Matt is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-19-2004, 08:41 PM   #13
Lane
Will code for food...
 
Join Date: Apr 2001
Location: Buckeye, AZ
Posts: 8,496
here is a little example of why order of the table is not the order of the inserted rows:


CREATE TABLE `a` (
`b` int(11) NOT NULL default '0'
) TYPE=INNODB;

INSERT INTO `a` ( `b` )
VALUES (
'0'
);


INSERT INTO `a` ( `b` )
VALUES (
'1'
);


INSERT INTO `a` ( `b` )
VALUES (
'2'
);

DELETE FROM `a` WHERE `b` = 1;


INSERT INTO `a` ( `b` )
VALUES (
'3'
);


SELECT *
FROM `a`;

this will give you:
0
3
2

not:
0
2
3


See what i mean?
__________________
Lane is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-19-2004, 09:43 PM   #14
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,115
I see I see said the blind man....

hmmm....
__________________
All cookies cleared!
sarettah is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks
Thread Tools



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.