在mybatis中使用case when進(jìn)行條件篩選判斷時遇到
Failed to process, please exclude the tableName or statementId.
這樣的報錯信息,報錯的信息是語法錯誤
但是我在mysql的命令行中運行sql語句是沒問題的
1
2
3
4
5
6
7
8
9
|
//我的 case when 語句 WHERE dept.type = 1 AND ( CASE agent.dept_type WHEN "agent" THEN dept.id=30 END ) //當(dāng)agent的dept_type為 "agent" 時,將添加dept.id = 30的判斷 |
這段sql語句在命令行內(nèi)運行沒問題但是放到mybatis上執(zhí)行就會報錯
1
2
3
4
5
6
7
8
|
//修改后 WHERE dept.type = 1 AND dept.id= ( CASE agent.dept_type WHEN "agent" THEN 30 END ) |
后來將dept.id放到外面就解決了這個問題
20190718-補(bǔ)充記錄 :遇到另一個問題,如果dept這個表是聯(lián)查來的有可能會沒有數(shù)據(jù),在dept無數(shù)據(jù)的時候我們就無法給dept.id賦上啥參數(shù)了,并且不可以影響原表數(shù)據(jù)的查詢,我改成了下面這樣:
1
2
3
4
5
6
7
8
9
|
//修改后 WHERE dept.type = 1 AND (dept.id= ( CASE agent.dept_type WHEN "agent" THEN 30 ELSE 0 END ) or dept.id is null ) |
添加dept.id為空的判斷即可
(在mysql語句里可以有很多方法解決,但是在mybatis上就會報錯 -_-||)
2019-7-30-補(bǔ)充說明:
如果是空字符串不可以使用""要改成單引號''
1
2
|
CASE WHEN *** THEN *** ELSE "" =>這樣也會報錯,需要改成=> ELSE '' |
補(bǔ)充:Mybatis case when test 注意事項
1
2
3
4
5
6
7
8
9
10
11
|
<choose> < when test= "groupBy!=null and groupBy==1" > p_id areaId, </ when > < when test= "groupBy!=null and groupBy==2" > c_id areaId, </ when > < when test= "groupBy!=null and groupBy==3" > r_id areaId, </ when > </choose> |
test 中 用 == 不能用 = ,否則報錯。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。如有錯誤或未考慮完全的地方,望不吝賜教。
原文鏈接:https://blog.csdn.net/guoqing2016/article/details/89233882