IF function in H2 for MySQL compatibility

前端 未结 3 779
梦谈多话
梦谈多话 2021-02-13 23:11

I\'m using H2 (with MySQL compatibility mode) to write some automated tests against our software that uses MySQL. Unfortunately, it seems like H2 does not have have the IF

相关标签:
3条回答
  • 2021-02-13 23:52

    Ended up just rewriting queries to use functions compatible with both database - H2, MySql. In my case, the functions in question were replaced with IFNULL.

    0 讨论(0)
  • 2021-02-13 23:55

    I've just had the same issue, and I resolved it with CASE/WHEN/THEN SQL statement. So you can rewrite your query as follows:

    SELECT CASE WHEN true THEN 'TRUE!!' ELSE 'FALSE!!!' END;
    

    Surely it's more verbose, but it fits both H2 and MySQL.

    0 讨论(0)
  • 2021-02-13 23:58

    Yes you can create the if function as an alias:

    CREATE ALIAS IF NOT EXISTS `IF` AS $$
        String ifFunction(boolean condition, String exp1, String exp2){
            if(condition) {
                return exp1;
            } else {
                return exp2;
            }
        }
    $$;
    
    0 讨论(0)
提交回复
热议问题