TINYINT UNSIGNED 数据列在 MacOS MySQL_8 的 where 子句中不能减去
The TINYINT UNSIGNED data column can't minus in where clause of MySQL_8 in MacOS
这是 MySQL(8.0.x) 运行 MacOS 中的数据:
ysql> desc t_room;
+--------------+-------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------------+------+-----+---------+-------+
| roomNum | varchar(60) | NO | PRI | NULL | |
| roomType | varchar(20) | NO | | NULL | |
| roomDesc | varchar(400) | YES | | NULL | |
| rentPayDay | tinyint unsigned | YES | | NULL | |
| rentFee | smallint unsigned | NO | | NULL | |
| asstMine | varchar(400) | YES | | NULL | |
| asstOwner | varchar(400) | YES | | NULL | |
| owner | varchar(30) | YES | | NULL | |
| signDate | date | YES | | NULL | |
| contract | mediumblob | YES | | NULL | |
| roomFacility | varchar(120) | YES | | NULL | |
+--------------+-------------------+------+-----+---------+-------+
11 rows in set (0.00 sec)
mysql> select rentPayDay from t_room;
+------------+
| rentPayDay |
+------------+
| 15 |
| 20 |
| 25 |
| 10 |
+------------+
4 rows in set (0.00 sec)
当我使用如下 where 子句进行查询时,一切正常:
mysql> select roomNum, owner, rentPayDay from t_room where rentPayDay - 10 <= 5;
+---------------+--------+------------+
| roomNum | owner | rentPayDay |
+---------------+--------+------------+
| xxxC-906 | John | 15 |
| xxxxx-908 | Doe | 10 |
+---------------+--------+------------+
2 rows in set (0.00 sec)
但是,当我减去一个可能大于某些 rentPayDay 值的值时,如下所示:
mysql> select roomNum, owner, rentPayDay from t_room where rentPayDay - 11 <= 5;
问题是这样的:
ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(`dev_learn`.`t_room`.`rentPayDay` - 11)'
有人对此有任何想法吗?为什么 TINYINT 数据不能在 where 子句中减去一个更大的数值?
一个UNSIGNED TINYINT
has a range 0-255,里面不能表示负值
如果您遇到转换错误,请考虑:
SELECT * FROM rooms WHERE CAST(rentPayDay AS DECIMAL) - 11 <= 5;
参考@Akina 和@Ken White,我对这个错误的猜测是:
the express {columnName OPERATOR -value-} still be restricted as a whole with the same definition of {columnName}.
我不知道为什么以及在 MySQL 对我的查询的响应中发生了什么。
解决这个问题的更好方法:
please refer to @Akina comment under my original question post.
非常感谢所有关心和帮助的人。
这是 MySQL(8.0.x) 运行 MacOS 中的数据:
ysql> desc t_room;
+--------------+-------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------------+------+-----+---------+-------+
| roomNum | varchar(60) | NO | PRI | NULL | |
| roomType | varchar(20) | NO | | NULL | |
| roomDesc | varchar(400) | YES | | NULL | |
| rentPayDay | tinyint unsigned | YES | | NULL | |
| rentFee | smallint unsigned | NO | | NULL | |
| asstMine | varchar(400) | YES | | NULL | |
| asstOwner | varchar(400) | YES | | NULL | |
| owner | varchar(30) | YES | | NULL | |
| signDate | date | YES | | NULL | |
| contract | mediumblob | YES | | NULL | |
| roomFacility | varchar(120) | YES | | NULL | |
+--------------+-------------------+------+-----+---------+-------+
11 rows in set (0.00 sec)
mysql> select rentPayDay from t_room;
+------------+
| rentPayDay |
+------------+
| 15 |
| 20 |
| 25 |
| 10 |
+------------+
4 rows in set (0.00 sec)
当我使用如下 where 子句进行查询时,一切正常:
mysql> select roomNum, owner, rentPayDay from t_room where rentPayDay - 10 <= 5;
+---------------+--------+------------+
| roomNum | owner | rentPayDay |
+---------------+--------+------------+
| xxxC-906 | John | 15 |
| xxxxx-908 | Doe | 10 |
+---------------+--------+------------+
2 rows in set (0.00 sec)
但是,当我减去一个可能大于某些 rentPayDay 值的值时,如下所示:
mysql> select roomNum, owner, rentPayDay from t_room where rentPayDay - 11 <= 5;
问题是这样的:
ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(`dev_learn`.`t_room`.`rentPayDay` - 11)'
有人对此有任何想法吗?为什么 TINYINT 数据不能在 where 子句中减去一个更大的数值?
一个UNSIGNED TINYINT
has a range 0-255,里面不能表示负值
如果您遇到转换错误,请考虑:
SELECT * FROM rooms WHERE CAST(rentPayDay AS DECIMAL) - 11 <= 5;
参考@Akina 和@Ken White,我对这个错误的猜测是:
the express {columnName OPERATOR -value-} still be restricted as a whole with the same definition of {columnName}.
我不知道为什么以及在 MySQL 对我的查询的响应中发生了什么。 解决这个问题的更好方法:
please refer to @Akina comment under my original question post.
非常感谢所有关心和帮助的人。