【LightDB】mysql字符和数值比较兼容

文章讨论了MySQL中字符与数值的比较规则,以及LightDB在不同版本对于这种比较的支持情况,强调了字符转数值时的处理差异和LightDB对MySQL兼容性的提升。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mysql字符和数值比较

mysql在进行字符和数值比较时,会将字符类型转换为数值类型进行比较。例如:

select * from test where 1 = '1';
select * from test where 2 > '12';
select * from test where '2' > '12'; -- 结果和示例3不同
select * from test where 2 > 12; -- 结果同示例3

通过上述sql验证了结论

mysql字符和数值转换规则

select * from test where 1 = '1a'; -- 1= '1a'-> true
select * from est where 1 = ''; -- 1= ''-> false
select * from est where 0 = ''; -- 0= ''-> true

通过以上测试可以看出,mysql模式下字符串转数字时,会忽略尾部不合法字符。

lightdb兼容mysql的字符和数字比较

lightdb在21.3版本已支持字符和数值的比较。和mysql一样,也是将字符转换成数值。但当字符中出现不合法字符时,会报错。23.4版本提供了这一支持。支持后效果如下:

lightdb@mysql=# select * from dual where 1 = '1a'; 
 dummy 
-------
 X
(1 row)

lightdb@mysql=# select * from dual where 1 = ''; 
 dummy 
-------
(0 rows)

lightdb@mysql=# select * from dual where 0 = ''; 
 dummy 
-------
 X
(1 row)
lightdb@mysql=# create TABLE INT2_NUMERIC_TBL(f1 smallint, f2 int, f3 bigint, f4 numeric(20,10),id serial);
CREATE TABLE
lightdb@mysql=# INSERT INTO INT2_NUMERIC_TBL VALUES ('34.5'::varchar,'34.5'::varchar,'34.5'::varchar,'34.5'::varchar);
INSERT 0 1
lightdb@mysql=# INSERT INTO INT2_NUMERIC_TBL VALUES ('2e2'::varchar,'2e2'::varchar,'2e2'::varchar,'2e2'::varchar);
INSERT 0 1
lightdb@mysql=# INSERT INTO INT2_NUMERIC_TBL VALUES ('2e2a'::varchar,'2e2a'::varchar,'2e2a'::varchar,'2e2a'::varchar); 
INSERT 0 1
lightdb@mysql=# INSERT INTO INT2_NUMERIC_TBL VALUES ('34.5','34.5','34.5','34.5');
INSERT 0 1
lightdb@mysql=# INSERT INTO INT2_NUMERIC_TBL VALUES ('2e2','2e2','2e2','2e2');
INSERT 0 1
lightdb@mysql=# select * from INT2_NUMERIC_TBL;
 f1  | f2  | f3  |       f4       | id 
-----+-----+-----+----------------+----
  35 |  35 |  35 |  34.5000000000 |  1
 200 | 200 | 200 | 200.0000000000 |  2
 200 | 200 | 200 | 200.0000000000 |  3
  34 |  34 |  34 |  34.5000000000 |  4
   2 |   2 |   2 | 200.0000000000 |  5
(5 rows)

lightdb@mysql=# select * from INT2_NUMERIC_TBL where f1 = '';
 f1 | f2 | f3 | f4 | id 
----+----+----+----+----
(0 rows)

lightdb@mysql=# select * from INT2_NUMERIC_TBL where f2 = '';
 f1 | f2 | f3 | f4 | id 
----+----+----+----+----
(0 rows)

lightdb@mysql=# select * from INT2_NUMERIC_TBL where f3 = '';
 f1 | f2 | f3 | f4 | id 
----+----+----+----+----
(0 rows)

lightdb@mysql=# select * from INT2_NUMERIC_TBL where f4 = '';
 f1 | f2 | f3 | f4 | id 
----+----+----+----+----
(0 rows)

lightdb@mysql=# 
lightdb@mysql=# select * from INT2_NUMERIC_TBL where f1 = '1a';
 f1 | f2 | f3 | f4 | id 
----+----+----+----+----
(0 rows)

lightdb@mysql=# select * from INT2_NUMERIC_TBL where f2 = '1a';
 f1 | f2 | f3 | f4 | id 
----+----+----+----+----
(0 rows)

lightdb@mysql=# select * from INT2_NUMERIC_TBL where f3 = '1a';
 f1 | f2 | f3 | f4 | id 
----+----+----+----+----
(0 rows)

lightdb@mysql=# select * from INT2_NUMERIC_TBL where f4 = '1a';
 f1 | f2 | f3 | f4 | id 
----+----+----+----+----
(0 rows)

lightdb@mysql=# 
lightdb@mysql=# select * from INT2_NUMERIC_TBL where f1 = '1e2';
 f1 | f2 | f3 | f4 | id 
----+----+----+----+----
(0 rows)

lightdb@mysql=# select * from INT2_NUMERIC_TBL where f2 = '1e2';
 f1 | f2 | f3 | f4 | id 
----+----+----+----+----
(0 rows)

lightdb@mysql=# select * from INT2_NUMERIC_TBL where f3 = '1e2';
 f1 | f2 | f3 | f4 | id 
----+----+----+----+----
(0 rows)

lightdb@mysql=# select * from INT2_NUMERIC_TBL where f4 = '1e2';
 f1 | f2 | f3 | f4 | id 
----+----+----+----+----
(0 rows)

lightdb@mysql=# select * from INT2_NUMERIC_TBL where f1 = '1e2a';
 f1 | f2 | f3 | f4 | id 
----+----+----+----+----
(0 rows)

lightdb@mysql=# select * from INT2_NUMERIC_TBL where f2 = '1e2a';
 f1 | f2 | f3 | f4 | id 
----+----+----+----+----
(0 rows)

lightdb@mysql=# select * from INT2_NUMERIC_TBL where f3 = '1e2a';
 f1 | f2 | f3 | f4 | id 
----+----+----+----+----
(0 rows)

lightdb@mysql=# select * from INT2_NUMERIC_TBL where f4 = '1e2a';
 f1 | f2 | f3 | f4 | id 
----+----+----+----+----
(0 rows)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值