JSqlParser解析SQL时SUM包裹IF出错

  • Post author:
  • Post category:其他




SQL

SELECT SUM(IF(1=1,1,0)) AS `result` FROM sys_user

这种会报错的。



错误信息

Caused by: net.sf.jsqlparser.JSQLParserException: null

at net.sf.jsqlparser.parser.CCJSqlParserUtil.parseStatements(CCJSqlParserUtil.java:154)

at com.baomidou.mybatisplus.core.parser.AbstractJsqlParser.parser(AbstractJsqlParser.java:64)

… 92 common frames omitted

Caused by: net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: “(” “(”

at line 10, column 19.

Was expecting one of:

“&”

“,”

“::”

“;”

“<<”

“>>”

“ACTION”

“ANY”

“AS”

“BYTE”

“CASCADE”

“CAST”

“CHAR”

“COLUMN”

“COMMIT”

“CONNECT”

“DO”

“ENABLE”

“END”

“EXCEPT”

“EXTRACT”

“FIRST”

“FOLLOWING”

“FOR”

“FROM”

“GROUP”

“HAVING”

“INDEX”

“INSERT”

“INTERSECT”

“INTERVAL”

“INTO”

“ISNULL”

“KEY”

“LAST”

“MATERIALIZED”

“MINUS”

“NO”

“NULLS”

“OPEN”

“ORDER”

“OVER”

“PARTITION”

“PERCENT”

“PRECISION”

“PRIMARY”

“PRIOR”

“RANGE”

“REPLACE”

“ROW”

“ROWS”

“SEPARATOR”

“SIBLINGS”

“START”

“TABLE”

“TEMP”

“TEMPORARY”

“TRUNCATE”

“UNION”

“UNSIGNED”

“VALUE”

“VALUES”

“WHERE”

“XML”

“ZONE”

“^”

“|”

<K_DATETIMELITERAL>

<S_CHAR_LITERAL>

<S_IDENTIFIER>

<S_QUOTED_IDENTIFIER>

at net.sf.jsqlparser.parser.CCJSqlParser.generateParseException(CCJSqlParser.java:18597)

at net.sf.jsqlparser.parser.CCJSqlParser.jj_consume_token(CCJSqlParser.java:18447)

at net.sf.jsqlparser.parser.CCJSqlParser.Statements(CCJSqlParser.java:516)

at net.sf.jsqlparser.parser.CCJSqlParserUtil.parseStatements(CCJSqlParserUtil.java:152)

… 93 common frames omitted



解决办法

将IF换成case when的形式



版权声明:本文为qq_15719169原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。