SELECT * FROM table ORDER BY date DESC LIMIT 3;
The above statement outputs the newest 3 entries but I want the oldest 3 without using ASC instead of DESC, like:
SELECT * FROM table ORDER BY date DESC BOTTOM 3;
Fetch the data that you want from the table by ascending order, and then query that subquery to reorder the data:
SELECT * FROM (
SELECT * FROM table ORDER BY date ASC LIMIT 3
) result
ORDER BY date DESC
Alternatively, you can also query the data in ascending order, and then programmatically reverse the order of the array, with something like array_reverse().
Related
SELECT *
FROM `user_job_application`
ORDER BY `user_job_application`.`user_id` DESC
It gives the table result like image preview.
but when user_idis same then, I want to fetch result order by user_job_application_date desc
We can ORDER results using multiple columns.
Try this:
SELECT *
FROM `user_job_application`
ORDER BY `user_job_application`.`user_id` DESC, user_job_application_date desc
SELECT * FROM `user_job_application`
ORDER BY `user_job_application`.`user_id` DESC,
`user_job_application`.`user_job_application_date` DESC;
Just put a comma after your DESC and add the next ORDER BY item.
I have a table named comments.I want to retrieve the last 3 rows in the ascending order of time.The following query fetches it in the descending order of time.What should I do to get the desired result?
select * from comments where id=1 order by time desc limit 0,3
Use a subquery:
select c.*
from (select c.*
from comments c
where id = 1
order by time desc
limit 0, 3
) c
order by time asc;
I have a table that has transactions with a datetime column. I'm trying to select the last 'n' records (i.e. 20 rows) but have it sorted oldest to newest.
SELECT *
FROM table
WHERE 1=1
ORDER BY table.datefield DESC
LIMIT 20;
Gives me the 20 most recent, but in the opposite order.
Is this possible in one query, or will I have to do a query to get total rows and then adjust the limit based on that so I can do the table.datefiled ASC and then limit (total rows - n), n
Building a SELECT around your original SELECT and convert this to a derived table should do it
SELECT t.*
FROM (
SELECT *
FROM table
WHERE 1=1
ORDER BY table.datefield DESC
LIMIT 20
) t
ORDER BY t.datefield
I am using this query but it is not working in mysql
SELECT TOP 1 * FROM (SELECT TOP 5 * FROM ads ORDER BY id DESC) ads ORDER BY id DESC
You can use ORDER BY ... LIMIT {[offset,] row_count | row_count OFFSET offset} (lets say you want to get 5 records from 10th - 10,11,12,13,14):
SELECT * FROM ads
ORDER BY id DESC
LIMIT 10,5
Although I assume you don't want to get them sorted by id but rather by views or similar criteria where ORDER BY views DESC would take a place (don't forget to to add index on views count).
The 'TOP' does not function on MySQL. You can edit the query in the following manner to get the job done
SELECT * FROM (SELECT * FROM ads ORDER BY id DESC LIMIT 5) ads ORDER BY id DESC LIMIT 5
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