I need to get the information from DB in such a way, the limit should be 3 rows and out of which i want to sort by descending order.
I used
select * from table where coloumn = 'Myfilter' order by serialNumber desc limit 3
after the execution I am not getting the latest three records rather the first three records ordered by descending.
Applying limit before order by
SELECT * FROM (SELECT * FROM table WHERE coloumn = 'Myfilter' ORDER BY serialNumber LIMIT 3) a ORDER BY serialNumber DESC
This query solves my question thank you all for suggestions,
SELECT * FROM (SELECT * FROM table WHERE coloumn='myFilter' ORDER BY serialnumber desc LIMIT 3) a ORDER BY serialnumber asc
the query uses to select the latest 3 rows ordered by big to small serial number then again the selected rows order where reversed, thnx #Kelvin Barsana
"SELECT * FROM table WHERE coloumn = 'Myfilter' ORDER BY serialNumber DESC LIMIT 3";
Related
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 have got a problem with getting top n records from database and ordering them by a specific column and paginating them.
For example I want first 100 movies from movie table and order these first 100 records by name and display 10 records per page.
However this doesn't work;
SELECT name FROM movies ORDER BY id DESC, name DESC, LIMIT 0,10
I am quite confused here. In order to paginate I have to use LIMIT in such ways;
LIMIT 0,10 = FIRST PAGE
LIMIT 10,20 = SECOND PAGE
and so on.
In order to get first records, I use ORDER BY id DESC however when I want to list FROM Z to A, ORDER BY id DESC, name DESC doesn't do the trick.
In another words what I want to do is to get first (latest) 100 records out of 10.000 and order this 100 records by name (ASC or DESC) and / or by view (ASC or DESC).
I hope I was clear enough to explain my problem.
I will be glad if you could help me out with this one.
Shift the order of your order by statements
(updated)
select name
from ( select *
from movies
order by id desc
limit 100 )
order by name desc
limit 0,10
It uses the first one first and if equal it looks at the next one
You could try to use
select name from
(select name from movies order by id desc limit 0,100)
order by name desc limit 0,10
Wrap your select in another select;
SELECT name FROM (SELECT * FROM movies ORDER BY id DESC LIMIT 0,100) ORDER BY name DESC LIMIT 0,10
Edit: Updated limits.
I have a table made up of podcasts I have done, song, artist, title mix number date and etc. I am using a class with individual methods to display them in separate
My question is, how do you get desc rows? Like this (mix_number is an int)
select mix_name, date where mix_number = MAX(mix_number) limit 1 //1st query
select mix_name, date where mix_number = MAX(mix_number - 1)//this would be the second query
select mix_name, date where mix_number = MAX(mix_number - 2)//3rd query
I dont want to hardcode the where clause with a number because I want it to update as I add more.
I am really asking is MAX(mix_number) or MAX(mix_number-1) proper? I cant get it to work this way
I hope this is understandable. I have other queries in the methods but an answer here will fix them too.
select mix_name, date
FROM tableName
where mix_number = (SELECT MAX(mix_number) FROM tableName)
LIMIT 1
or
select mix_name, date
FROM tableName
where mix_number = (SELECT MAX(mix_number) FROM tableName) - 1
LIMIT 1
You need an ORDER BY clause.
select mix_name, date order by mix_number desc
Adding LIMIT 0, 1 will be your first query. LIMIT 1, 1 will be second. And so on
First query:
SELECT mix_name, date
FROM table
ORDER BY mix_number DESC
LIMIT 1, 0
Second query:
SELECT mix_name, date
FROM table
ORDER BY mix_number DESC
LIMIT 1, 1
If you want all rows from the same query:
SELECT mix_name, date
FROM table
ORDER BY mix_number DESC
If you want to more precisely sort rows with the same mix_number:
SELECT mix_name, date
FROM table
ORDER BY mix_number DESC, date DESC
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 have a table full of users and, using the below query, I'm able to get the result of the highest score divided by points:
SELECT MAX(points/score) FROM table
However, I'd like to grab the user associated with the result; any suggestions?
SELECT *
FROM table
ORDER BY points/score DESC
LIMIT 1
just order by points/score and limit the result to one:
SELECT
*
FROM
table
ORDER BY
points/score DESC
LIMIT 1