Union查询结果数据类型转换问题

根据以下说法,数据由低优先级向高优先级转化:

When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence.

部分数据类型优先级顺序如下图:

用mysql进行测试,结果却不符合由低到高转换的说法:

mysql> select*from testu;
+---+---+
| a | b |
+---+---+
| 1 | a |
| 2 | b |
+---+---+
2 rows in set

mysql> create table `ttt` select `a` from `testU` union select `b` from `testU`;
Query OK, 4 rows affected
Records: 4  Duplicates: 0  Warnings: 0

mysql> select*from `ttt`;
+---+
| a |
+---+
| 1 |
| 2 |
| a |
| b |
+---+
4 rows in set

mysql> desc `ttt`;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| a     | varchar(40) | NO   |     |         |       |
+-------+-------------+------+-----+---------+-------+
1 row in set
从选出的结果来看,跟预计的由低优先级的varchar类型转成int型的情况并未发生,恰恰相反。

这个还是没弄懂是为什么。

猜你喜欢

转载自blog.csdn.net/qq_42254088/article/details/81872492