mysql 强转bigint类型_MySQL中的CASE强制将列数据类型设置为BIGINT而不是INT

bd96500e110b49cbb3cd949968f18be7.png

Well it would rather be a very simple thing but I couldn't figure out myself.

While trying the below line in Stored Procedure in MYSQL server, it converts or sets the column datatype to BIGINT instead of expected INT

CASE `L`.`Code` WHEN 'ABCD' THEN 0 ELSE `XL`.`ID` END as XLID

The magical thing is that if I remove the 0 from it, then it returns INT as datatype as expected. So this 0 is to blame for sure, but I couldn't understand why and how to resolve this? I tried to use CAST but it didn't allow to cast INT to INT.

wc0ev.jpg

Would be a great help.

解决方案

The following trick can help:

DELIMITER //

CREATE FUNCTION `return_integer`(`number` INT UNSIGNED)

RETURNS INT UNSIGNED DETERMINISTIC

BEGIN

RETURN `number`;

END//

DELIMITER ;

SELECT

CASE `L`.`Code`

WHEN 'ABCD' THEN `return_integer`(0)

ELSE `L`.`ID`

END AS XLID

FROM `table_name` `L`;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值