Ds.109
Ds.109

Reputation: 730

SQL ORDER BY not working

Get the latest 4 donations that come into the system .

Donation IDs are auto incremented (201 , 202, 203 , 204) .. therefore I wanted to order by DESC to get the latest 4 donations.

 $resultsdonations  = mysql_query("SELECT * FROM donationstable ORDER BY 'donation_id'  DESC LIMIT 0,4 ");

Upvotes: 0

Views: 1227

Answers (4)

eggyal
eggyal

Reputation: 125835

As the other answers have pointed out, you must remove the quotes from around donation_id.

As it currently stands, you are not sorting by the value in the donation_id column, but rather by the string literal 'donation_id', which is constant for each record and therefore results in an indeterminate ordering.

Upvotes: 0

micha
micha

Reputation: 49552

You don't need the quotes around donation_id. Just use .. BY donation_id DE ..

Upvotes: 0

Mosty Mostacho
Mosty Mostacho

Reputation: 43434

Remove the apostrophes around 'donation_id' and the 0 is unnecessary in the LIMIT clause considering your requirement:

$resultsdonations = mysql_query(
   "SELECT * FROM donationstable ORDER BY donation_id DESC LIMIT 4");

Upvotes: 2

Przemyslaw Kruglej
Przemyslaw Kruglej

Reputation: 8113

Try this:

$resultsdonations  = mysql_query("SELECT * FROM donationstable ORDER BY donation_id  DESC LIMIT 0,4 ");

(drop the ' around the donation_id column)

Upvotes: 0

Related Questions