How do I SELECT the 5 highest values in an integer column? - mysql

What is the correct SQL statement if i wish to select the biggest 5 integer rows in the column "id"?
Currently i have something which is only getting id that are less than 5:
SELECT * FROM table WHERE id < 5

SELECT * FROM mytable ORDER BY id DESC LIMIT 5

5 largest "id" values:
SELECT * FROM table ORDER BY id DESC LIMIT 5

If I understand you correctly , i think you need this,
SELECT * FROM table ORDER BY id DESC LIMIT 5

SELECT top 5 * FROM mytable ORDER BY id DESC

Related

How to get the end of an Ascending list of records with a limit?

I'm fighting a bit with a query I'm building. Let's say I've got a DB table like this:
id | some_string
----------------
1 | 'lala'
2 | 'jeje'
3 | 'poopoo'
4 | 'wicked wicked'
I now want to get the last three records (2, 3, and 4) ordered ascending by key. I tried this:
SELECT * FROM tableName LIMIT 3 ORDER BY id ASC
This gets me the first three records, instead of the last three. I can of course also use the query below, which gets me the correct records, but then I don't get them in Ascending order:
SELECT * FROM tableName LIMIT 3 ORDER BY id DESC
Does anybody know how I can get the last three records in an ascending order? All tips are welcome!
select * from (
select * from table_name order by id desc limit 3
) last_3_rows
order by id
Sort on the resulting result set ie. do a select * from (<your query here>) order by id
This is a query inside another query. that reorders your query.(SQL - How to reorder a select query that uses the limit constraint)
select * FROM (SELECT * FROM tableName LIMIT 3 ORDER BY id DESC) AN_UNUSUAL_NAME ORDER BY id ASC

MySQL fetch 10 randomly rows where 3 predefined rows needs to be part of the results and be at the beginning

I have a table with ID's from 1 to 20 and I need to fetch 10 rows (random), but 3 of the 10 rows are predefined and needs to be at the beginning of the result list - In one MySQL statement:
This works, but the production table contains over 500K rows:
SELECT id
FROM tableName
WHERE id IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
ORDER BY FIELD(id, 5,6,7) DESC, RAND()
LIMIT 10
I would need something like this:
SELECT id
FROM tableName
WHERE id IN (5,6,7,*)
ORDER BY FIELD(id, 5,6,7) DESC, RAND()
LIMIT 10
... what would be the right syntax?
You can use statement like this:
SELECT
id
FROM
(
SELECT
id,
IF(id IN (5,6,7),1,0) AS priority
FROM tableName
) t
ORDER BY priority DESC, RAND()
LIMIT 10
If UNION ALL is allowed, you could try something like this:
select id from tablename where id in (compulsary_1, compulsary_2, compulsary_3)
order by id desc
union all
select id from tablename where id not in (compulsary_1, compulsary_2, compulsary_3)
order by rand() limit 7
Playing around... I found this and also works:
SELECT id
FROM tableName
WHERE id IN (5,6,7) OR id > 0
ORDER BY FIELD(id,5,6,7) DESC, RAND()
LIMIT 10

Select last N rows from MySQL

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

How do I select the 1st row from a selection of 10 random rows

Lets say I have a 1000 rows in my table.
I want to select 10 of those at random.
SELECT * FROM table ORDER BY RAND() LIMIT 10
Then I want to select the row in that result with the highest value for number
SELECT * FROM table ORDER BY number DESC LIMIT 1
Can anyone help me come up with an efficient way of doing this?
Just use a subquery:
SELECT *
FROM (
SELECT * FROM table ORDER BY RAND() LIMIT 10
)
ORDER BY number DESC LIMIT 1

Mysql select first then limit

How can i select the newest 5 rows from mysql (newest by id for example) then order them asc or desc ??
the problem is i have 1,2,3,4,5,6,7 , i want to select the newest 2 for example (6,7) then order them asc or desc (6,7 or 7,6) , but if i use the orderby then the limit it would be (1,2) or (7,6) , is there anyway to do it from ONE sql statement ?
thank you
You may use something like
SELECT * FROM TABLE WHERE ID IN (SELECT ID FROM TABLE WHERE X ORDER BY ID LIMIT 2) ORDER BY ID DESC
I didn't get if you wanted the newest 5 or 2, I use 2 here. But here is something I just wrote without testing.
SELECT * FROM table WHERE id IN (SELECT id FROM table ORDER BY id DESC LIMIT 2) ORDER BY id ASC/DESC;
Hope this will help! :)
ok i found the solution , thank you all for your efforts and answers ,
i made 2 columns , ID and TSTAMP (for timestamp) , just made it like order by id asc , tstamp asc/desc ... limit x , so it orders always by the id then orders by the tstamp and limits the right thing .
and nested queries are a pain for the engine ...
thanks
Select * from TABLE order by id asc , time_stamp desc
Why can't you do something such as
SELECT *
FROM TABLE
WHERE (TABLE.primary_key IN (
SELECT TABLE.primary_key
FROM TABLE
ORDER BY TABLE.id ASC
LIMIT 2
))
ORDER BY TABLE.id DESC