11

I have a basic write to and retrieve SQL database PHP "thing" and I want to have the output in descending order. How do I do that?

For example, the last entry is shown first, then the entry before that, then the entry before that, etc. The first entry ever is last.

1
  • Wow so weird to see your own question from years in the past. You should have a primary ID, that way you can actually do an ordered return. Some cases you don't have an ID/key so you would have to grab all of them, then do the ordering client side. If you do have an ID, when you do a call such as ORDER BY ID DESC WHERE ID < :spec_limit LIMIT 15 where :spec_limit is a returned last_row ID with your call, then a button can call the next 15 from the last known id (assuming data is fixed) Commented Jan 26, 2018 at 2:23

7 Answers 7

16

Use:

SELECT field_name
FROM table_name
ORDER BY id DESC

By default, MySQL will show results in ascending order. If you want to show them in reverse order, use ORDER BY field_name DESC.

You can use id or date as the field name.

Sign up to request clarification or add additional context in comments.

Comments

4

Change the ORDER BY statement

  • from ORDER BY col or ORDER BY col ASC or to ORDER BY col DESC
  • from ORDER BY col DESC to ORDER BY col ASC

7 Comments

I'm sorry to pull a noob moment here but I have not defined an index, so does this mean that ordering isn't possible until I create the index? I have tried both order by id desc and order by col desc, unless it is the capitalization?
Yeah I'm getting a "Warning: mysqli_fetch_array()expects parameter 1 to be mysqli_result, boolean given in... "
An index is not necessary to use ORDER BY.
Okay. I have fixed the problem following the multiple answers provided here. My mistake was not using the name of the field.
huh, the order isn't actually working... the newest entries are randomly positioned in between previous entries
|
3

Sort using DESC ORDER BY.

SELECT * FROM <TABLE> ORDER BY <COLUMN> DESC

Comments

3

If there's an auto increment field, you order by that field, descending.

SELECT "column_name"
  FROM "table_name"
  [WHERE "condition"]
  ORDER BY "column_name" [ASC, DESC]; 

That's from ORDER BY Clause - Sort Data In SQL.

Comments

1

Write the below query to retrieve data:

SELECT * FROM `table_name` order by id desc

It will retrieve data from the table in descending order.

2 Comments

Thank you for your response. I am going to attempt implementing it now. Would you happen to know if you can actually format a PHP output in the sense that let's say for a given td, you enter : vertical-align: top; would the content posted in this td actually align to the top?
I am not sure. I wasn't entirely sure if the website is following the code by the index file or what is in the retrieve.php files which also have divs like .boxed_tabgreen for example which has properties like width, height, padding... I mean I think it is working.
1

MySQL general query syntax for SELECT:

SELECT field1, field2,...fieldN FROM table_name
[WHERE Clause][GROUP BY]
[ORDER BY][LIMIT]

Example query 1:

SELECT id,first_name,last_name,email FROM users ORDER BY first_name desc // In this case you can only fetch data by = id,first_name,last_name,email

Example query 2:

SELECT * FROM users ORDER BY first_name desc // In this case all fields will be selected

Note: ORDER BY ASC = Data will sort by Ascending Order & ORDER BY DESC = Data will sort by descending Order

1 Comment

Is "//" valid for comments in SQL?
1

Another possible answer if you had the same requirement as me where I needed the newest 24 entries but in reverse order:

SELECT T.* FROM(SELECT * FROM 'table' [WHERE 'condition'] [ORDER BY dbId DESC] LIMIT 24) as T ORDER BY T.dbId ASC;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.