我有一个简单的查询,它选择了几行,有两个内连接,并有几个where语句。
然而,WHERE g.company BETWEEN '2' AND '21'
行没有像我期望的那样工作。
当前代码
SELECT
g.date_reg AS Date_Entered,
g.userid AS UserID,
x.account AS Account_Number,
g.company AS Company_Number
FROM gtable g
INNER JOIN MSI f ON g.vt= f.vt AND g.company = f.company AND g.vn = f.vn
INNER JOIN grow x ON f.vn = x.vn AND f.cb = x.cb AND (f.dac = x.ca OR f.dad = x.da)
WHERE g.company BETWEEN '2' AND '21'
AND g.vn = '123123456'
AND g.year = '2014'
使用当前代码,我得到以下结果。
Date_Entered UserID Account_Number Company_Number
01/01/2014 AB01 1112 20
如果我将该行更改为WHERE g.company BETWEEN '1' AND '21'
,我会收到以下信息:
Date_Entered UserID Account_Number Company_Number
03/03/2014 CD01 1312 19
02/02/2014 BG01 1412 19
01/01/2014 AB01 1112 20
这没有意义,因为我只改变了下限。
然后更奇怪的是,如果我将其更改为WHERE g.company BETWEEN '0' AND '21'
我得到以下内容:
Date_Entered UserID Account_Number Company_Number
03/03/2014 CD01 1312 02
02/02/2014 BG01 1412 02
03/03/2014 CD01 1312 03
02/02/2014 BG01 1412 05
03/03/2014 CD01 1312 05
02/02/2014 BG01 1412 05
03/03/2014 CD01 1312 06
02/02/2014 BG01 1412 06
03/03/2014 CD01 1312 07
02/02/2014 BG01 1412 07
03/03/2014 CD01 1312 07
02/02/2014 BG01 1412 07
03/03/2014 CD01 1312 07
02/02/2014 BG01 1412 08
03/03/2014 CD01 1312 08
02/02/2014 BG01 1412 08
03/03/2014 CD01 1312 09
02/02/2014 BG01 1412 09
03/03/2014 CD01 1312 19
02/02/2014 BG01 1412 19
01/01/2014 AB01 1112 20
我想从where声明中显示任何公司的数字介于2和21之间(包括2和21)。
答案 0 :(得分:2)
WHERE g.company BETWEEN '0' AND '21'
我认为g.company来自char组(例如,类型为VARCHAR2); '0' AND '21'
来自char group => Oracle使用ASCII代码比较字符串。在这种情况下
' 0' < ' 1' < ' 2' < ......< ' 9' => ' 2' > ' 1 [其他]'
如果你确定g.company
只包含数字值,你可以试试这个
WHERE g.company BETWEEN 0 AND 21
(Oracle应隐式将g.company
转换为数字)
或者这个:
WHERE to_number(g.company) BETWEEN 0 AND 21
如果g.company
不仅可以包含数字值(并且您不需要这些值),那么您可以尝试这样做:
WHERE case when regexp_like(g.company, '^[[:digit:]]+$') then to_number(g.company) else -1 end BETWEEN 0 AND 21