MySql查询,选择大于 [英] MySql Query, Select greater than

查看:261
本文介绍了MySql查询,选择大于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为faq_questions的表,其结构如下:

I've got a table, called faq_questions with the following structure:

id int not_null auto_increment,
question varchar(255),
sort_order int

我正在尝试构建给出给定排序顺序的查询,选择具有第二高排序顺序的行.

I'm attempting to build a query that given a sort order, selects the row with the next highest sort order.

示例:

id  question                sort_order
1   'This is question 1'    10
2   'This is question 2'    9
3   'This is another'       8
4   'This is another one'   5
5   'This is yet another'   4

好吧,所以想像一下我将5传递给我已知的排序顺序(id 4),我需要它返回ID为3的行.由于不能保证sort_order是连续的,所以我不能只选择known_sort_order + 1

Ok, so imagine I pass in 5 for my known sort order (id 4), I need it to return the row with id 3. Since there's no guarantee that sort_order will be contiguous I can't just select known_sort_order + 1.

谢谢!

推荐答案

这似乎太简单了,但是看起来像您所需要的:

It seems too simple, but it looks like what you need:

SELECT id,question FROM `questions` 
WHERE `sort_order` > sort_order_variable
ORDER BY sort_order ASC 
LIMIT 1

这篇关于MySql查询,选择大于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆