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...)
Related
I have a mysql query which will return all the details from table along with i need max_row count i.e total no of rows in a table using COUNT(*) in a single select query without using cross join.
Note: MySQL version is earlier version of 8
Query :
SELECT * FROM tablename ORDER BY column name DESC LIMIT 0,10;
The total count of a table is simple, when you want to add it to every row.
SELECT
*
,(SELECT COUNT(*) FROM tablename ) count1
FROM tablename
ORDER BY column name
DESC LIMIT 0,10;,
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 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 table with records 'username' and 'balance'. How to show 10 usernames with highest balance?
Extra: How to show but only when they have more than 1.000.000$?
My Table:
You just need to sort the balance first and limit the result by 10:
SELECT `username`, `balance`
FROM `table_name`
ORDER BY `balance` DESC
LIMIT 10
These are very basic SQL statements. You probably should find a good SQL tutorial and spend some time playing with the various SELECT clauses. In your case:
Just order your results in descending balance and limit to 10 records?
SELECT username FROM mytable ORDER BY balance DESC LIMIT 10
Add a WHERE condition to filter for only those records with a balance over your specified threshold:
SELECT username FROM mytable WHERE balance >= 1000000
SELECT username FROM mytable WHERE balance>1000000 ORDER BY balance DESC LIMIT 10
Try with this:
select username, balance from table_name where balance >= 1000000 order by balance DESC LIMIT 10