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
Related
I want to select one row from the latest 30 rows based on the date.
And with the below it is selecting the random row from all the rows and thats not what I want.
SELECT * FROM product where userId=1 ORDER BY RAND() LIMIT 1
So how can I select from the latest 30? Thanks.
A sub-select will return the last 30, and then you can choose a random row from there
SELECT pr.* FROM
( SELECT * from product
WHERE pr.userId=1
ORDER BY created_at DESC
LIMIT 30 ) AS pr
ORDER BY RAND()
LIMIT 1
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 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
Is it possible to get comma separated value of first n (say 10 rows of a column) rows using Mysql?
I have a query to get data greater than CURDATE(). And it will return more than 100 rows of result. What I want is, GROUP_CONCAT the first 10 rows of result.
This is my query:
SELECT GROUP_CONCAT(user_id) AS userids
FROM user_tasks
WHERE due_date > CURDATE() LIMIT 10;
am getting entire rows. I need first 10 rows only
Thanks
Use subquery:
SELECT
GROUP_CONCAT(user_id) AS userids
FROM
(SELECT
user_id
FROM
user_tasks
WHERE due_date > CURDATE()
LIMIT 10) AS users
You need to use a sub query to impose the limit, like this:
SELECT GROUP_CONCAT(sub_query.user_id) AS userids
FROM
(
SELECT user_id
FROM user_tasks
WHERE due_date > CURDATE()
LIMIT 10
) sub_query
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