Fetching the last 3 rows in mysql in ascending order - mysql

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;

Related

MariaDB opposite of limit

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().

MySQL - Order by one column for a limit then by another column for a limit

I'm developing an application that displays rows which have columns "Time created" and "Number of Likes".
I'm trying to return the first 5 results of my query with the rows that have the most likes, and then the remaining 95 by most recent date, without duplication.
My current query only accomplishes ordering by date.
"SELECT * FROM `$category` ORDER BY time DESC LIMIT 100"
Is what I'm attempting to do possible in one query? Or will I have to perform two queries and filter out duplicate rows?
UNION automatically removes duplicate rows and LIMIT outside the parethesis applies to the whole query.
(SELECT *, like_count AS likes FROM category ORDER BY like_count DESC LIMIT 5)
UNION
(SELECT *, 0 AS likes FROM category ORDER BY time DESC)
ORDER BY likes DESC, time DESC
LIMIT 100

MySQL select top ten records with no duplicate uid

I have the following table (user_record) with millions of rows like this:
no uid s
================
1 a 999
2 b 899
3 c 1234
4 a 1322
5 b 933
-----------------
The uid can be duplicate .What I need is to show the top ten records(need inclued uid and s) with no duplicate uid order by s (desc). I can do this by two steps in the following SQL statements:
SELECT distinct(uid) FROM user_record ORDER BY s DESC LIMIT 10
SELECT uid,s FROM user_record WHERE uid IN(Just Results)
I just wana know is there a bit more efficient way in one statement?
Any help is greatly appreciated.
ps:I also have following the SQL statement:
select * from(select uid,s from user_record order by s desc) as tb group by tb.uid order by tb.s desc limit 10
but it's slow
The simpliest would be by using MAX() to get the highest s for every uid and sorted it based on the highest s.
SELECT uid, MAX(s) max_s
FROM TableName
GROUP BY uid
ORDER BY max_s DESC
LIMIT 10
SQLFiddle Demo
The disadvantage of the query above is that it doesn't handles duplicates if for instance there are multiple uid that have the same s and turn out to be the highest value. If you want to get the highest value s with duplicate, you can do by calculating it on the subquery and joining the result on the original table.
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT DISTINCT s
FROM TableName
ORDER BY s DESC
LIMIT 10
) b ON a.s = b.s
ORDER BY s DESC

MySQL select random rows with order by

I want to retrieve random rows from table but this rows must be order in category.
select category,
(select order_number
from orders
where order_number in (123,125,128,129,256,263,966,258,264,159,786)
order by rand())
from orders
order by category
This is the query I tried. But that retrieves whole data in table.
Worked query ;
SELECT category,order_number FROM (
SELECT category,order_number
from orders
where order_number in (`$order_numbers_variable`)
order by rand()
) order by category
I assume the requirement is:
Retrieve 'N' random rows from a table sorted by 'category'.
Lets assume N is 10. If you want to change the number of rows, then change it in the LIMIT clause.
SELECT * FROM (
SELECT category from orders ORDER BY rand() ASC LIMIT 10
) AS innerResult
ORDER BY innerResult.category

Fetching top 5 records of maximum salary

I have 3 columns with id,usrnameand salary. I want to find maximum salary of 5 records.
How will i write query in mysql ?
In MySQL you can use ORDER BY to sort the rows in descending order and use LIMIT to return only the top 5 rows:
SELECT id, usrname, salary
FROM yourtable
ORDER BY salary DESC
LIMIT 5
You have to use LIMIT, like so:
SELECT * FROM mytable ORDER BY salary DESC LIMIT 5