英文:
MySQL math on text in select list should fail
问题
如何让MySQL在尝试在SELECT列表中执行文本的数学运算时引发错误(而不仅仅是发出警告)。例如:
SELECT 'foo' * 'bar';
我希望该查询失败。
英文:
How can I make MySQL throw an error (not simply issue a warning) when I try and perform a mathematical operation with text in the SELECT list. For example:
SELECT 'foo' * 'bar';
I want that query to fail.
答案1
得分: 1
你可以定义一个类似这样的函数:
CREATE DEFINER=`root`@`localhost` FUNCTION `multiply`(a float, b float) RETURNS float
NO SQL
BEGIN
RETURN a*b;
END
然后 select multiply('foo','bar');
会返回一个错误,而 select multiply(2,3);
仍然返回正确的答案。
但缺点是你需要使用这个函数而不是正常的乘法方式。
英文:
You could define a function like this:
CREATE DEFINER=`root`@`localhost` FUNCTION `multiply`(a float, b float) RETURNS float
NO SQL
BEGIN
RETURN a*b;
END
Then select multiply('foo','bar');
will return an error, while select multiply(2,3);
still returns the correct answer.
But the downside is that you need to use this function in stead of the normal way of multiplying.
答案2
得分: 0
对于数据更改语句(INSERT/UPDATE等),这些类型的操作将会产生错误,假设你已启用了STRICT_TRANS_TABLES sql_mode,这已经是自MariaDB 10.2.4和MySQL 5.7.5以来的默认设置。
对于非数据更改语句,请让你的客户端检查是否有警告;具体细节取决于你正在使用的客户端。
英文:
For data altering statements (INSERT/UPDATE/etc), these types of operations will already produce an error, assuming you have the STRICT_TRANS_TABLES sql_mode enabled, which has been the default since mariadb 10.2.4 and mysql 5.7.5.
For non-data altering statements, have your client check if there was a warning; details depend on what client you are using.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论