Why do I get a syntax error when using CAST in MySQL?

前提是你 提交于 2019-12-10 12:57:41

问题


I am using MySQL workbench v5.2.44 CE. I am running it against a local MySQL 5.5 install.

I am trying to use the CAST function, but keep getting the following error:

syntax error, unexpected INT_SYM

It doesn't matter what the source and target date types are. The only time it doesn't give me an error is when the target datatype is DECIMAL. Here is an example:

SELECT CAST(IFNULL(comboCount, 1) * COUNT(partID) AS INT) INTO comboCount
FROM productOption

I have tried everything, but nothing seems to work.


回答1:


Try to do the math outside:

SELECT CAST(IFNULL(comboCount, 1) AS INT) * COUNT(partID) INTO comboCount
FROM productOption

If that doesn't work, try to CAST as UNSIGNED; not INT.



来源:https://stackoverflow.com/questions/14531087/why-do-i-get-a-syntax-error-when-using-cast-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!