Welcome,
I'm wondering is it possible to reverse returned data in sorting "order by desc" but i want that data in reverse order.
For example, i got table with values
ID
1
2
3
4
And i do
Order by ID ASC LIMIT 3
I got
1
2
3
When i do Order by ID DESC limit 3
i get
4
3
2
I would like to have
3
2
1
So i would like to order by ASC but revers results.
I was always doing this in PHP side using array_reverse, but today i want ask You.
Maybye i'm wrong and i can do this just in Mysql.
Regards
SELECT *
FROM (
SELECT ...
FROM ...
ORDER BY ID ASC
LIMIT 3
) AS sq
ORDER BY ID DESC
Think of it as working in two steps. First it executes the inner query: selects 3 records with lowest IDs. Then in the outer query it sorts them in descending order.
You can fetch the first three rows using a subquery and then reverse the order of these rows in an outer query:
SELECT *
FROM
(
SELECT *
FROM yourtable
ORDER BY ID
LIMIT 3
) T1
ORDER BY ID DESC
Nope, you aren't wrong. There are no difference for any sensible amount of data. Array_reverse is all right.
That's not a thing you have to be concerned too much of. Just use whatever you like more - for readability or other subjective reasons
You can do this with a sub query :
SELECT * FROM
(SELECT * FROM myTable ORDER BY idMyTable LIMIT 0, 3) AS r
ORDER BY r.idMyTable DESC
Resources :
Mysql - Subqueries
You could use an outer SELECT to reverse the order:
SELECT *
FROM (
SELECT …
ORDER BY id ASC
LIMIT 3
) sub
ORDER BY id DESC
Related
my table keep million records.
i just want to extract the 5 most counter rows in last 200 rows.
Im using this but as i thinking this is two time select .. that may not right for best performace plase advice.
"select * from (
select sid,title,catid,counter from table_stories
where catid=1 order by sid desc limit 200
) astemps order by counter desc limit 5"
thanks very much..
regards
This is your query:
select *
from (select sid, title, catid, counter
from table_stories
where catid = 1
order by sid desc
limit 200
) astemps
order by counter desc
limit 5;
For optimal performance, you want an index on table_stories(catid, sid desc). You can throw title and counter into the index, but they won't help much. Unfortunately, MySQL may not take advantage of the descending key for to replace the sort.
I have got this MYSQL
SELECT * FROM chat WHERE To_='$NameId' OR From_='$NameId' ORDER BY `DATE` DESC LIMIT 0,50
This code returns me data in this order
1 newest
2 newest
3 newest
4 newest
5 newest
But i want data to be returned in this way
5 newest
4 newest
3 newest
2 newest
1 newest
How can i do that?
Use this :
SELECT * FROM (select * from chat where To_='$NameId' OR From_='$NameId' ORDER BY DATE DESC LIMIT 0,50) sub ORDER BY DATE ASC
As I understand you want to change direction of records after getting first 50 records.
You can use sub-query feature of MySQL. Assume that result of your query is new, shiny table. So write SQL for desired result using this table (actually it is data set). (In our case we rename it as results)
SELECT * FROM (select * from chat where To_='$NameId' OR From_='$NameId' ORDER BY DATE DESC LIMIT 0,50) as results ORDER BY results.DATE ASC
Also if you have "count" of results you can do same thing using single-level query. Think about that;
SELECT * FROM chat WHERE To_='$NameId' OR From_='$NameId' ORDER BY `DATE` ASC LIMIT ***MyCount***,50
MySQL will not force you to rename your sub-query results but renaming
them results in more readable SQL codes.
I want to select last 50 rows from MySQL database within column named id which is primary key. Goal is that the rows should be sorted by id in ASC order, that’s why this query isn’t working
SELECT
*
FROM
`table`
ORDER BY id DESC
LIMIT 50;
Also it’s remarkable that rows could be manipulated (deleted) and that’s why following query isn’t working either
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
Question: How is it possible to retrieve last N rows from MySQL database that can be manipulated and be in ASC order ?
You can do it with a sub-query:
SELECT * FROM
(
SELECT * FROM table ORDER BY id DESC LIMIT 50
) AS sub
ORDER BY id ASC;
This will select the last 50 rows from table, and then order them in ascending order.
SELECT * FROM table ORDER BY id DESC LIMIT 50
save resources make one query, there is no need to make nested queries
SELECT * FROM table ORDER BY id DESC, datechat DESC LIMIT 50
If you have a date field that is storing the date (and time) on which the chat was sent or any field that is filled with incrementally (order by DESC) or de-incrementally (order by ASC) data per row put it as second column on which the data should be ordered.
That's what worked for me!!!! Hope it will help!!!!
Use it to retrieve last n rows from mysql
Select * from tbl order by id desc limit 10;
use limit according to N value.
if anyone need this
you can change this into
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
into
SELECT
*
FROM
`table`
WHERE
id > (SELECT MAX(id)- 50 FROM chat)
ORDER BY id ASC;
select * from Table ORDER BY id LIMIT 30
Notes:
* id should be unique.
* You can control the numbers of rows returned by replacing the 30 in the query
I want to create a random image banner which fetches the image data from a MySQL database.
And I only want to fetch the latest three records, randomly.
How can I fetch the the 3 most recent records, in a random order?
I am the following query:
SELECT * FROM bottom_advt WHERE bottom_advt_page_name='News' ORDER BY RAND() LIMIT 1
but it's not working.
You said 3 records. So try:
SELECT * FROM bottom_advt WHERE bottom_advt_page_name='News' ORDER BY RAND() LIMIT 3
You're limiting your results to one with LIMIT 1. Change it to LIMIT 3 to get three results.
Try this if you want one of the last three records:
SELECT * FROM bottom_advt WHERE bottom_advt_page_name='News' AND id+3>last_insert_id() ORDER BY RAND() LIMIT 1
You could use this:
SELECT *
FROM bottom_advt
WHERE ID IN (SELECT * FROM (
SELECT id
FROM bottom_advt
WHERE bottom_advt_page_name='News'
ORDER BY id DESC
LIMIT 3) last)
ORDER BY RAND()
LIMIT 1
Subquery will return the last 3 id that have bottom_advt_page_name='News', and the outer query will randomly select one of those.
Please see fiddle here.
I have a table called "reports"
it looks like:
user_id | report_post
1 2
2 2
3 2
4 10
Now I want to list the first three entries at first, cause the reported post id "2" is 3 times in this table... i want so sort them by the maximum of entries.
Hope you guys understand... thanks alot
----edit------
my output have to look like this
report_post | entries
2 3
10 1
Select report_post, Count(1) As Entries
From reports
Group By Report_Post
Order By Count(1) DESC
SELECT *
FROM (
SELECT user_id, report_post, COUNT(*) AS cnt
FROM reports
GROUP BY report_post
) c
ORDER BY cnt DESC
With your edit, this does what you're asking:
select report_post, count(*) entries
from reports
group by report_post
order by entries desc
Use a subquery. This should do the trick in MySQL:
select * from reports
order by (
select count(*) from reports as reports_a
where reports_a.report_post=reports.report_post
) desc;
(the above answer's your question before you edited it to change it's meaning)
For the edited question, it is a trivial example of a group by:
select report_post,count(*) as entries
from reports
group by report_post
order by entries desc;
SELECT report_post as report_post, count(report_post) as entries
FROM `reports` group by `report_post`
Single query.
SELECT * FROM reports ORDER BY entries DESC
I'm a little unsure of what exactly you are asking. Are you just trying to get it to return entries where the report_post id is equal to "2"?
If that is the case, this should work:
SELECT * FROM reports WHERE report_post=2;
Sorry if I misunderstood your question.