Quick BI使用SQL生成数据集报错“check the manual that corresponds to your MySQL server version for the right syntax to use near 'outer join hk on XX.id = xxx.xx_id full outer join tf on xx.id = xxx.xx”
更新时间:
问题描述
Quick BI中使用SQL创建数据集,运行报错“check the manual that corresponds to your MySQL server version for the right syntax to use near 'outer join hk on XX.id = xxx.xx_id full outer join tf on xx.id = xxx.xx”完整的异常堆栈信息如下:
errMsg:数据源执行SQL失败:INTERNAL: java.lang.RuntimeException: SQL execute error by datasource... java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'outer join hk on ht.id = hk.contract_id full outer join tf on ht.id = tf.co' at line 45 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1200) org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) 问题原因
根据错误的提示信息看:在OUTER JOIN时,靠近某个特定位置的语法不正确。具体问题出现在对FULL OUTER JOIN的使用上,因为MySQL数据库直到8.0版本才原生支持FULL OUTER JOIN。如果使用的是MySQL 8.0之前的版本,这可能是导致错误的原因。
解决方案
确认MySQL版本:首先,确保了解您所使用的MySQL服务器版本。如果版本低于8.0,FULL OUTER JOIN将不被直接支持。
替代方案:对于不支持FULL OUTER JOIN的MySQL版本,可以采用LEFT JOIN和UNION或者两个LEFT JOIN结合COALESCE或IFNULL函数来模拟FULL OUTER JOIN的效果。例如:
SELECT
COALESCE(ht.id, hk.contract_id, tf.id) AS id,
ht.*, hk.*, tf.*
FROM
ht
LEFT JOIN hk ON ht.id = hk.contract_id
LEFT JOIN tf ON ht.id = tf.contract_id
UNION
SELECT
hk.contract_id AS id,
ht.*, hk.*, tf.*
FROM
hk
LEFT JOIN ht ON ht.id = hk.contract_id
WHERE ht.id IS NULL
UNION
SELECT
tf.contract_id AS id,
ht.*, hk.*, tf.*
FROM
tf
LEFT JOIN ht ON ht.id = tf.contract_id
WHERE ht.id IS NULL;升级MySQL:如果条件允许,考虑将MySQL升级到8.0或更高版本以直接支持FULL OUTER JOIN语法。
请根据您的实际情况选择合适的解决策略,并调整SQL语句以符合您的数据库版本和需求。
适用于
Quick BI
该文章对您有帮助吗?