3

I have this piece of code:

return $this->db->query("SELECT * FROM `messages` where `toid`='$userid' and `fromid`='$userConvensation' or `fromid`='$userid' and `toid`='$userConvensation' order by `id` DESC LIMIT 10");

It takes the 10 latest messages.

But i need the 10 latest messages, but also in reversed order. how am i able to do that?

output code:

foreach($messages->result() as $thisMessage) {
//dostuff
}
2
  • 1
    ("SELECT * FROM messages` where toid='$userid' and fromid='$userConvensation' or fromid='$userid' and toid='$userConvensation' order by id DESC LIMIT 10") AS latest ORDER BY id ASC? Commented Sep 29, 2015 at 2:16
  • 1
    @aldrin27, why don't you put that into an answer ? Commented Sep 29, 2015 at 2:22

2 Answers 2

1

how about subquery:

SELECT * FROM `messages` WHERE id IN (
  SELECT `id` FROM `messages` where `toid`='$userid' and
  `fromid`='$userConvensation' or `fromid`='$userid' and
  `toid`='$userConvensation' order by `id` DESC LIMIT 10
)
ORDER BY `id` ASC
Sign up to request clarification or add additional context in comments.

2 Comments

for some reason now. its not selecting it by the toid and fromid, or only getting 10 rows .prntscr.com/8lqcxd
Error Number: 1235 This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
0

Wrap your query and renamed it and reorder that.

("SELECT * FROM messages` where toid='$userid' and fromid='$userConvensation' or fromid='$userid' and toid='$userConvensation' 
 order by id DESC LIMIT 10") AS latest ORDER BY id ASC

1 Comment

for some reason now. its not selecting it by the toid and fromid.prntscr.com/8lqcxd

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.