0

I would like to construct a mysql_query that will give me only new rows inserted in a table with a where condition like ....

SELECT (LAST 5) FROM `x`  WHERE `USER_KEY` = '$RK' ORDER BY `dd` DESC 

this data will be parsed in json afterwards,otherwise i would have just got all the data and looped threw.

1 Answer 1

2

you can reverse the order and get the first 5. That is the same as getting the last 5 and sorting the other way around

SELECT * FROM `x`  WHERE `USER_KEY` = '$RK' ORDER BY `dd` ASC limit 5

or if you need the desc order you can do

select * from (SELECT * FROM `x`  
               WHERE `USER_KEY` = '$RK' 
               ORDER BY `dd` asc 
               limit 5) as x 
order by `dd` desc
Sign up to request clarification or add additional context in comments.

1 Comment

Surely your second example will select the first 5? You'd need to use the first query as the subquery and then apply an ORDER BY in the parent query simply to change the order of the results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.