MySQL和Laravel-之前和之后检查现有记录

时间:2018-11-17 15:11:36

标签: mysql laravel

检查我正在查找的记录是否存在的最简单方法是什么?例如,当我搜索ID = 2的记录时,就有前一个ID = 1的记录,但是当我搜索ID = 1的记录时,之前没有记录(与搜索最后一个记录ID一样,没有下一个记录)。 / p>

1 个答案:

答案 0 :(得分:0)

我创建了SQL查询:

SELECT * FROM `table` 
WHERE 
`id` = (select `id` from table WHERE `id` > 0 and `id` < 2 ORDER by `id` desc limit 1) or 
`id` = 2 or 
`id` = (select `id` from table where `id` > 2 and `id` < (select `id` from table ORDER by `id` desc limit 1) limit 1)

“ 2”将通过代码传递,查询将根据需要向我提供3条ID为1,2,3的记录,现在我只需要将其“转换”为正确的laravel雄辩版本即可