我想在字符串列上使用"BETWING"子句进行搜索.在做一些测试时,我得到了这个:

让我们假设有一个国家/地区表,其"name"列的类型为varchar.如果我执行此查询:

Select * from country where name between 'a' and 'b'

我得到的结果是:

Argentina
.
.
.
Argelia.

它排除了那些以B开头的国家,我觉得有点奇怪.

有没有更准确的方式进行这种搜索?进行这次搜索还有其他的 idea 吗?

提前谢谢你

推荐答案

表达式

name between 'A' and 'B'

相当于

name>='A' and name<='B'

So 'Argentina' is >='A' and <='B' and it satisfies the condition. But 'Bolivia' is NOT <='B'. 'Bolivia'>'B'. It doesn't just look at the first letter: it looks at the whole string. Which is surely the way it ought to be: if it didn't do this, there'd be no way to say that you wanted a range that included 'Smith' but not 'Smithers'.

要实现你想要的,你可以说:

substr(name,1,1) between 'A' and 'B'

或者:

name like 'A%' or name like 'B%'

或者:

name>='A' and name<'C'

Database相关问答推荐

使用存储过程从子表中删除数据

包含接受Cassandra中多个数据的语句

为什么Hibernate会为@JoinTable的双向@OneToMany关系生成一个复杂的子查询?

删除Postgres中的JSONB列并在不停机的情况下回收空间

为什么postgres枚举需要4个字节?

Prisma - 将属性的类型设置为枚举数组

如何将初始数据放入数据库

任何必要的可空外键示例?

仅对表中的一列授予更改

MongoDB和Redis有什么区别?

Spring Boot:如何使用多个模式并在运行时动态 Select 使用哪一个

如果限制在本地机器上,最好使用 R 和 SQL

返回 DataSet/DataTable 的 PowerShell 函数中的奇怪行为

从 Java 创建 MySQL 数据库

在 Firestore 中使用嵌套的单个查询

如何在没有验证提取的情况下将 MSSQLServer 数据库提取为 .dacpac?

如何在 SQL Server 中创建数据库的别名

Web 应用程序的文件存储:文件系统、数据库和 NoSQL 引擎

python3k中的sqlite3中的cursor.rowcount总是-1

在 Django 中,如何从数据库中 Select 100 条随机记录?