![]() |
![]() |
![]() |
||||
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. |
![]() ![]() |
|
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed. |
|
Thread Tools |
![]() |
#1 |
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 |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#2 |
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.
__________________
;-)!;-)!;-)!;-)!;-)!;-)!;-)!;-)!;-)!;-)!;-) |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#3 |
making it rain
Industry Role:
Join Date: Oct 2003
Location: seattle
Posts: 22,119
|
Every table should have a unique id field.
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#4 |
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! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#5 |
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
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#6 | |
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:
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#7 |
Confirmed User
Join Date: Mar 2003
Location: Toronto, Ontario
Posts: 4,402
|
Create a field for a timestamp.
Use it.
__________________
![]() Paul |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#8 | |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,115
|
Quote:
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! |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#9 | |
Confirmed User
Join Date: Feb 2003
Location: Sacramento
Posts: 1,751
|
Quote:
__________________
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 |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#10 | |
Confirmed User
Join Date: Jun 2003
Location: CZ, EU
Posts: 1,363
|
Quote:
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#11 |
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.
__________________
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#12 | |
So Fucking Banned
Join Date: Aug 2003
Location: San Diego, CA
Posts: 5,464
|
Quote:
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#13 |
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?
__________________
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#14 |
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! |
![]() |
![]() ![]() ![]() ![]() ![]() |