How to Reverse Order Output of a MySQL Query

How can I select rows in reverse order in MySQL?

Or you could do it without caring about the deleted results. Just get the fourth before the given id (6) by:

SELECT * FROM SomeTable WHERE id < 6 ORDER BY id DESC LIMIT 4,1

Reverse order of SQL query output

You can reorder that resultset by doing an outer select and ordering it the way you want (ASC):

SELECT * FROM (
select * from LastResult ORDER BY Date DESC LIMIT 10
) as
ORDER BY Date ASC;

If this fails (as it would in PostgreSQL 9.3.4), use an alias for the inner select to make it as below:

SELECT * FROM (
select * from LastResult ORDER BY Date DESC LIMIT 10
) as foo
ORDER BY foo.Date ASC;

PHP Reverse the order of results from MySQL DB

You have two solutions:

  1. Sort your links descending
  2. Use array_reverse or rsort

Solution #1:

"SELECT * FROM notfi1 WHERE Own='" .$_GET['u']. "' ORDER BY UserId DESC"

Solution #2:

$result = mysql_query("SELECT * FROM notfi1 WHERE Own='" .$_GET['u']. "'");
while($row = mysql_fetch_array($result))
{
$data[] = $row['UserId'];
}
rsort($data);
foreach($data as $item){
echo 'link:<a href=member.php?u=' .$row['UserId']. '>text</a><br>';
}

Second method is better because it means you are seperating your data retrieval from your display... IT SHOULD be done this way but doesn't prevent you to sort your data on the MySQL Server

How to reverse a MySQL result

You can just wrap your query in another select and reverse the order by:

SELECT res
FROM (
SELECT `date`,SUM(DISTRIBUTED_AMOUNT / EXCHANGE_RATE) AS res
FROM royalties
GROUP BY `date`
ORDER BY `date` DESC LIMIT 12
) a
ORDER BY `date` ASC

How to make execution update reverse query mysql by id?

Created a table num_sample with numbers from 1 to 100. Defined a primary key on the column num. And wrote this update

update num_sample set num = num + 1 order by num desc limit 100;

Worked absolutely fine in MySQL.

records showing in reverse order in mysql

I think you should run the following query.

Select * from (
SELECT * FROM `tblmessages` order by message_id desc limit 10
) as a
order by message_id

mysql reverse order of records fetching

A subquery would be formatting like this:

select * from(
select *
from
(select "1" as s,`to` as f, message as m,sent
from table1 where chat.`to` = "user1"
union
select "1" as s,`to` as f, message as m,sent
from table1 where chat.`from` = "user1" ) u
ORDER BY sent DESC
limit 5) o order by sent

As @GGio notes however, refactoring your query to use an OR would be more efficient:

select * from (
SELECT "1" as s, `to` as f, message as m, sent
FROM table1
WHERE chat.`to` = "user1" OR chat.`from` = "user1"
ORDER BY sent DESC
LIMIT 5) o
order by sent

MySQL Order Previous 6 Months In Reverse

I think this is will help

SELECT month(c.date) AS month_num, monthname(c.date) AS month, SUM(value) AS 
total
FROM commissions c
WHERE c.date BETWEEN CURDATE() - INTERVAL 7 MONTH AND CURDATE()
GROUP BY month(c.date)
ORDER BY c.date DESC

How to reverse the output of EXTRACT() in MySQL

In the output2, the day, hour, and minute have one digit. So you can't reliably parse that back to YYYY-MM-DD HH:MM:SS.

I suggest you instead use DATE_FORMAT() and its reverse, STR_TO_DATE().

SELECT NOW(6) AS output1, DATE_FORMAT(NOW(6), '%Y%m%d%H%i%s%f') as output2
+----------------------------+----------------------+
| output1 | output2 |
+----------------------------+----------------------+
| 2021-12-06 20:44:31.927978 | 20211206204431927978 |
+----------------------------+----------------------+


Related Topics



Leave a reply



Submit