oracle 提供相当于Oracle NVL的mysql函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1227487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysql function to provide equivalent to Oracle NVL
提问by Stewart Robinson
I have a strange situation. I have to use NVL(columna, columnb) in Oracle and MySQL. I can't change the SQL as it is in a package I can't edit but it is the only thing that doesn't work in the application I have between MySQL and Oracle.
我有一个奇怪的情况。我必须在 Oracle 和 MySQL 中使用 NVL(columna, columnb)。我无法更改 SQL,因为它在我无法编辑的包中,但它是我在 MySQL 和 Oracle 之间的应用程序中唯一不起作用的东西。
How would I write NVL() in MySQL. I've looked here (http://dev.mysql.com/doc/refman/5.0/en/create-function-udf.html) and it looks like I have to write it in C and link it to MySQL.
我将如何在 MySQL 中编写 NVL()。我看过这里(http://dev.mysql.com/doc/refman/5.0/en/create-function-udf.html),看起来我必须用 C 编写它并将其链接到 MySQL。
However http://dev.mysql.com/doc/refman/5.0/en/create-procedure.htmlseems to say I can do it without compiling an add on.
但是http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html似乎说我可以在不编译插件的情况下做到这一点。
I've tried this but it doesn't work. What am I doing wrong?
我试过这个,但它不起作用。我做错了什么?
CREATE FUNCTION NVL (first DATETIME, second DATETIME)
RETURNS DATETIME
begin
DECLARE first DATETIME;
DECLARE second DATETIME;
DECLARE return_value DATETIME;
SET return_value = IFNULL(first,second);
RETURN return_value;
end
回答by Quassnoi
Use COALESCE
, it's much more simple:
使用COALESCE
,就简单多了:
DELIMITER $$
CREATE FUNCTION NVL (first DATETIME, second DATETIME)
RETURNS DATETIME
BEGIN
RETURN COALESCE(first, second);
END
$$
DELIMITER ;
回答by Cody Caughlan
I think the COALESCE() method is what you need.
我认为 COALESCE() 方法是您所需要的。
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce