首页 > 解决方案 > 使用 between 子句时 Sql 查询没有返回所有值

问题描述

我有一个查询:

select *
from indicatordetails
where indicator.month between 'January'and 'April'

当我运行它时,它不返回任何结果,但是当我使用时:

select *
from indicatordetails
where indicator.month between 'January'and 'march'

它返回结果。

注意:我的表格从一月到四月有结果,我会感谢任何帮助,谢谢

标签: sql

解决方案


你所拥有的“月”是一个字符串。的操作数between是有序的。作为字符串,'January''April'.

用数字更容易看出这一点。这个构造:

where x between 9 and 1

永远不会返回任何结果,因为 9 > 1。如果有匹配的值,这可能会返回结果:

where x between 1 and 9

我的建议是仅将此列与inor一起使用=

where indicator.month in ('January', 'February', 'March', 'April')

推荐阅读