获取大于和小于主ID的记录

时间:2015-08-13 05:46:35

标签: php mysql codeigniter

我有铅表,其中lead_id是主要ID,我想在单个查询中获取下一个lead_id和前一个lead_id .Lead_id是自动递增但是lead_id与用户有关系。是否可以在单个查询中获取数据?

3 个答案:

答案 0 :(得分:1)

可能你可以尝试类似的东西

(select lead_id from lead where lead_id < 4 order by lead_id DESC limit 1)    
union 
(select lead_id from lead where lead_id > 4 order by lead_id ASC limit 1)

答案 1 :(得分:0)

试试这个

select * from lead where 
      lead_id = (select min(lead_id) from lead where lead_id > 4) AND 
      lead_id = (select max(lead_id) from lead where lead_id < 4)

答案 2 :(得分:0)

Haven没有测试过这段代码,但是应该是类似下面的内容,其中$ lead_id是你带有一个名为lead的表的php lead_id

$query="SELECT * FROM lead WHERE lead_id =($lead_id+1) OR lead_id=($lead_id -1)";