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
Related
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().
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";
I need a MYSQL query to select only latest 10 records order by insertDate, but I can't fix limit 10 because maybe some record have had same date and I want all column data in latest 10 distinct insertDate rows.
Use a sub-query to find 10'th latest insertDate:
select * from tablename
where insertDate >= (select DISTINCT insertDate from tablename
order by insertDate desc limit 9,1)
order by insertDate desc
I'm not sure if you want that DISTINCT in the sub-select or not. (I guess not...)
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 mySQl db (name "stocks") with 50 tables, each tables with
id, symbol, date, time, open, high, low, close, volume as columns (9 columns).
I would like to know what is the last record for each table, ordered for date then time.
Should I have to ORDER BY all data for each table or there is a better way to just know last record?
I am asking help for a query that just return only last record for each table in db.
Thanks
PS For last record I mean most recent as Date then Time
There are two options how to do that:
-- I would use this only if you need more than one records
SELECT * FROM table ORDER BY date DESC LIMIT 1;
-- Way to go:
SELECT * FROM table WHERE date = (SELECT MAX(date) FROM table) LIMIT 1;
Don't forget to add index on date. If it's possible you add lot's of records at the same time you will have to add:
ORDER BY id DESC -- In case that date is highest for records for last records
ORDER BY time DESC -- Every other case
To the end of query
I am going to make the assumption that the record with the largest ID is the "last" (assuming strictly increasing sequential IDs that are unique within a table). If you have a better definition of "last" that could make a difference.
To get one "last" record, you could do:
Select * from table_1 where id = (select max(id) from table_1);
To get the results of all 50 tables into a single result set, you could do:
Select * from table_1 where id = (select max(id) from table_1)
union
Select * from table_2 where id = (select max(id) from table_2)
union
Select * from table_3 where id = (select max(id) from table_3)
union...
A MySQL-specific solution could be
Select * from table_1 order by id desc limit 1
union
Select * from table_2 order by id desc limit 1
union
Select * from table_3 order by id desc limit 1
union...
Based on your edit (where you actually define what you mean by "last"):
Select * from table_1 order by date desc, time desc, id desc limit 1
union
Select * from table_2 order by date desc, time desc, id desc limit 1
union
Select * from table_3 order by date desc, time desc, id desc limit 1
union...
Here is one way to do it without sorting the table:
select * from tab1
where time = (select max(time)
from tab1
where date = (select max(date) from tab1))
and date = (select max(date) from tab1)
It should be very fast, like, O(c), provided that both columns are indexed, otherwise the time will simply be O(n)