How to retrieve all records after one with an ID in symfony?

浪子不回头ぞ 提交于 2019-12-20 04:23:05

问题


Let's say I have a table that I sort alphabetically in symfony. Each item has a rather random id and I'd like to retrieve all items after a certain ID. For example:

Name      ID
------------
Apple     5
Banana    9
Coconut   3
Date      1
Eggplant  8

Let's say I wanted to retrieve Date and Eggplant. I know beforehand of the ID 3 and that I want everything after it.

How should I construct the statements to achieve this?

I don't care if the answer uses Propel, MySQL, SQL, or whatever. As long as it's possible.


回答1:


SELECT *
FROM tbl1
WHERE name > (
  SELECT name
  FROM tbl1
  WHERE id = 3
)
ORDER BY name

(runs at least with Postgres and with Just Aguy's SQL Fiddle)




回答2:


Here is a SQLFiddle that shows this working. The query is simple, just do some matching on the first letter. I don't like the idea of the hardcoded value of 3 being there. Is there a process that's going to set the value of the id?

    select name from tbl1 
    where (Left(name,1)) > (select Left(name,1) 
                        from tbl1 where id = 3)
    order by name asc


来源:https://stackoverflow.com/questions/15148365/how-to-retrieve-all-records-after-one-with-an-id-in-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!