更新查询中的转换函数问题
Issue with cast function in update query
这是一个简单的 table 和测试数据,我将使用它来演示我的问题:
create table foo(
id INTEGER AUTO_INCREMENT PRIMARY KEY,
orderpos int not null
);
insert into foo (orderpos) values (1);
insert into foo (orderpos) values (2);
insert into foo (orderpos) values (3);
insert into foo (orderpos) values (4);
insert into foo (orderpos) values (5);
我想用这样的查询更新一些字段:
update foo
set orderpos =
CAST(
CASE
WHEN id = 2 THEN 4
WHEN id = 3 THEN 8
END
AS INTEGER
)
where id in(2, 3);
但是我得到了错误
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTEGER
)
where id in(2, 3)'
我知道删除 CAST 查询会起作用,但我想了解为什么不允许此操作?
MySQL 是 5.6
这里是link到fiddlehttp://sqlfiddle.com/#!9/420d7f
不需要 cast()
。只需使用:
set orderpos = (CASE WHEN id = 2 THEN 4
WHEN id = 3 THEN 8
END)
如果您确实需要 cast()
,请使用 signed
或 unsigned
。
确实在这种情况下您不需要使用 CAST()。
但郑重声明,错误是因为您在语法不支持的位置使用 INTEGER
。
确定:
CAST(<expr> AS SIGNED)
CAST(<expr> AS SIGNED INTEGER)
CAST(<expr> AS UNSIGNED)
CAST(<expr> AS UNSIGNED INTEGER)
错误:
CAST(<expr> AS INTEGER)
请参阅 CAST()
和 CONVERT()
的文档以了解支持的数据类型语法:https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html
这是一个简单的 table 和测试数据,我将使用它来演示我的问题:
create table foo(
id INTEGER AUTO_INCREMENT PRIMARY KEY,
orderpos int not null
);
insert into foo (orderpos) values (1);
insert into foo (orderpos) values (2);
insert into foo (orderpos) values (3);
insert into foo (orderpos) values (4);
insert into foo (orderpos) values (5);
我想用这样的查询更新一些字段:
update foo
set orderpos =
CAST(
CASE
WHEN id = 2 THEN 4
WHEN id = 3 THEN 8
END
AS INTEGER
)
where id in(2, 3);
但是我得到了错误
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTEGER
)
where id in(2, 3)'
我知道删除 CAST 查询会起作用,但我想了解为什么不允许此操作?
MySQL 是 5.6 这里是link到fiddlehttp://sqlfiddle.com/#!9/420d7f
不需要 cast()
。只需使用:
set orderpos = (CASE WHEN id = 2 THEN 4
WHEN id = 3 THEN 8
END)
如果您确实需要 cast()
,请使用 signed
或 unsigned
。
确实在这种情况下您不需要使用 CAST()。
但郑重声明,错误是因为您在语法不支持的位置使用 INTEGER
。
确定:
CAST(<expr> AS SIGNED)
CAST(<expr> AS SIGNED INTEGER)
CAST(<expr> AS UNSIGNED)
CAST(<expr> AS UNSIGNED INTEGER)
错误:
CAST(<expr> AS INTEGER)
请参阅 CAST()
和 CONVERT()
的文档以了解支持的数据类型语法:https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html