Czechnology
Czechnology

Reputation: 15012

Skip the first row if a condition is met

I'm pulling some data from a forum and use it to display comments on my site.

The first row (post) might come from a dummy username but it doesn't have to.

Example:

id username  post
-------------------------
9   dummy    Hello World!
12  user     hi!
...

OR

id username  post
-------------------------
14  user1    hi!
19  user2    hello!
...

Now let's say I want to get the first ten rows/posts but not first row should not be retrieved if it comes from the dummy user. The dummy user may post another post later and such posts should be retrieved (so a simple WHERE username != 'dummy' doesn't do the trick).

Is there a simple way to accomplish this with a single SQL query? Solution using two queries is obvious.

Thanks!

Upvotes: 2

Views: 1531

Answers (1)

RichardTheKiwi
RichardTheKiwi

Reputation: 107826

select * from posts
where id >= (
   select id
   from posts
   where username <> 'dummy'
   order by id asc
   limit 1)
   order by id asc
limit 10

Take 10 posts, starting from the first non-dummy post

Upvotes: 2

Related Questions