gpt4 book ai didi

sql - 'between' 运算符在 oracle 中的不同行为

转载 作者:行者123 更新时间:2023-12-02 06:01:07 25 4
gpt4 key购买 nike

我知道“between”运算符包含指定的范围..但在以下情况下它的工作方式不同

我有具有以下属性的表 customer。

  customer 
{
customername varchar2(30),
custid integer(10,0)

}

查询

   select *  from customer c where  c.customername between  'a'  and 'b';

以上查询仅获取以“a”开头的客户名称的数据。但当我们在这种情况下将“介于”运算符与数字一起使用时,两者都包含在内。谁能向我解释一下这个行为。

最佳答案

没有什么不同。 BETWEEN 仍然包含在内。但是您应该记住,编程中的字符串比较比整数比较稍微复杂一点。在数据库中字符串是这样排序的:

'a' < 'andy' < 'andy1' < 'anna' < 'b' < 'boris' < 'brian'.

因此 在 'a' 和 'b' 之间 将返回:

'a', 'andy', 'andy1', 'anna', 'b' 

但是由于您没有完全命名为 'b' 的客户,您只能得到:

'a', 'andy', 'andy1', 'anna' 

如果您只需要那些名字以 a 或 b 开头的客户,您应该使用 SUBSTR :

select *  from customer c where  SUBSTR(c.customername, 1, 1) between  'a'  and 'b';

关于sql - 'between' 运算符在 oracle 中的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23733902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com