简体   繁体   English

如何从MySQL中的两列中查找范围

[英]How to find a range from two columns in mysql

I have a value say 45. 我的值是45。

I want to check whether this value is present in database or not. 我想检查此值是否存在于数据库中。

The problem is that I have two columns, say: 问题是我有两列,分别是:

--------------------
range_from  range_to
--------------------
   10 ------------ 20
--------------------
   21 ------------ 30
--------------------
   31 ------------ 40
--------------------
   41 ------------ 50
--------------------

How can I find this value? 如何找到该值?

SELECT * FROM Score WHERE range_from='' AND range_to=''
SELECT *
FROM score
WHERE range_from < 45 AND range_to > 45;

Normally you would use the keyword 'BETWEEN' 通常,您会使用关键字“ BETWEEN”

SELECT * FROM score 
WHERE 45 BETWEEN range_from AND range_to

To take care of boundary conditions also we can consider small correction in the above answer 为了照顾边界条件,我们也可以在上述答案中考虑进行较小的校正

SELECT *
FROM score
WHERE range_from <= 50 AND range_to >= 50;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM