2011-03-15 65 views
2

我从论坛中提取一些数据并使用它在我的网站上显示评论。如果符合条件,则跳过第一行

第一行(帖子)可能来自虚拟用户名,但它不必。

例子:

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

OR

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

现在,让我们说我想要得到的前十行/职位,但如果它来自虚拟用户不是第一行不应该被检索。虚拟用户可能会在稍后发布另一篇文章,并且应该检索这些文章(因此,简单的WHERE username != 'dummy'不会执行此操作)。

有没有简单的方法来完成这与 SQL查询?使用两个查询的解决方案很明显

谢谢!

回答

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

取10个职位中,从第一个非空后

+0

清洁容易,感谢出发! – Czechnology 2011-03-15 20:27:19