是真正的运算符

问题描述 投票:0回答:1

IS TRUE
SQL 运算符应该有多严格?例如,BigQuery 只允许实际的
BOOL
值,而我见过的所有其他方言也都允许数字,例如 mysql 或 postgres 允许
select '1' is true
,尽管这可能只是出于历史原因,因为许多数据库不包含实际的
BOOL
数据类型。

那么

IS TRUE
运算符应该允许什么作为操作数?

sql database-agnostic
1个回答
0
投票

严格按照 SQL-99 标准,

IS
只能与
TRUE
FALSE
UNKNOWN
进行比较。阅读 https://sql-99.readthedocs.io/en/latest/chapters/09.html 了解详细信息。

任何供应商的实施都可能与标准有所不同。例如,在 MySQL 中,true 只是整数 1 的别名,而 false 是整数 0 的别名。

mysql> select true is true;
+--------------+
| true is true |
+--------------+
|            1 |
+--------------+
1 row in set (0.05 sec)

mysql> select 1 is true;
+-----------+
| 1 is true |
+-----------+
|         1 |
+-----------+
1 row in set (0.03 sec)

mysql> select '1' is true;
+-------------+
| '1' is true |
+-------------+
|           1 |
+-------------+
1 row in set (0.03 sec)

SQLite 还允许数据类型具有一定的灵活性。

sqlite> select true is true;
1
sqlite> select 1 is true;
1
sqlite> select '1' is true;
1

PostgreSQL 介于两者之间。字符串“1”被视为 true,但整数 1 不是有效的操作数:

postgres=# select true is true;
 ?column? 
----------
 t
(1 row)

postgres=# select '1' is true;
 ?column? 
----------
 t
(1 row)

postgres=# select 1 is true;
ERROR:  argument of IS TRUE must be type boolean, not type integer
LINE 1: select 1 is true;
               ^

SQL 实现总是有自己的特性。您需要研究您使用的各个品牌的文档,甚至确切的版本。

© www.soinside.com 2019 - 2024. All rights reserved.