Mysql select first then limit - mysql

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

Related

Mysql select rows except first row

I have issue there with select from table. I want to select all rows except for first row. So .. There is my code
SELECT * FROM table ORDER BY id DESC
So this code select and order id's from table which give me id feedback "5>4>3>2>1". And there is issue .. How I can select and echo just 4>3>2>1 rows.
So if I had rows with id's 1,2,6,8,10 , echo will be 10,8,6,2,1 and I want select to echo just 8,6,2,1.
There is my full wrong code for select.
$other = mysql_query("SELECT * FROM table ORDER BY id DESC LIMIT 1, 1");
This should do it.
SELECT * FROM table WHERE id NOT IN (SELECT MAX(id) FROM table) ORDER BY id DESC
Try this:
SELECT *
FROM
(
SELECT *, row_number()
OVER (ORDER BY id DESC) row
FROM table
)
WHERE row != 1
It gives numbers to your selected rows and takes all of them without the one with row number 1
Try this
$other = mysql_query("SELECT * FROM table ORDER BY id DESC OFFSET 1");
THE ABOVE QUERY WONT WORK AS A LIMIT IS NEEDED
Refer to this answer
All you need is offset 1, but offset cannot be used without limit. So, I'd suggest something like:
SELECT * FROM table ORDER BY id DESC LIMIT 99999999 OFFSET 1
Warning: make sure your table doesn't contain lots of records, otherwise you will run into performance issues. Or, change the limit to something reasonable, like 10.
EDIT:
Read: How to use offset without limit
SELECT *
FROM table
WHERE id NOT IN ( SELECT id
FROM table
ORDER BY id DESC
LIMIT 1 )
ORDER BY id DESC;
You can try this.
In this case I am selecting all the rows, except the one with the biggest id.
In the example of [1,2,3,4,5], it will be :
SELECT *
FROM table
WHERE id NOT IN ( 5 )
ORDER BY id DESC;
Hope this helps!

How to select from a selection?

I'm creating a commenting system, which will have 2 top comments.
How can I select the latest 20 rows, and then from that selection, select the top 2 rows (likes-dislikes)? I can do it with a PHP loop, but it would not be as efficient. Currently I am just selecting the top 2 from the all the comments, but the two top comments never change, since people just up-vote those ones:
SELECT * FROM pagecomments WHERE page_id='$pageid' ORDER BY likes-dislikes DESC LIMIT 2
EDIT: The table is ordered by the the column "id", which is auto_increment. page_id is the page on the site. Sorry.
Nest your query:
SELECT *
FROM (
SELECT *
FROM pagecomments
WHERE page_id='$pageid'
ORDER BY date DESC
LIMIT 20
) t
ORDER BY likes-dislikes DESC
LIMIT 2
Order not only by LIkes, first order by date entered or timestamp. By ordering by date, you assure that you`ll get the latest 20 posts or comments.
SELECT * FROM pagecomments WHERE page_id='$pageid' ORDER by date_entered desc ,
likes-dislikes DESC limit 2
Since your id column is set to auto_increment, use it in a subquery:
select *, likes-dislikes
from (
select *
from pagecomments
where page_id='$pageid'
order by id desc
limit 20
) t
order by likes-dislikes desc
limit 2
Condensed SQL Fiddle Demo

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 5 highest values in an integer column?

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