Mybatis空值關(guān)聯(lián)的問題解析及解決方案
一.問題描述
1.已知條件
已知 table_1 有 3 個(gè)字段 order_no,community_id,post_id
已知 table_2 也有 3 個(gè)字段 order_no,community_id,post_id
2.關(guān)聯(lián)條件
現(xiàn)在需要將 table_1 和 table_2 進(jìn)行關(guān)聯(lián),關(guān)聯(lián)條件是 order_no,community_id,post_id 這 3 個(gè)字段,但是 order_no 不為 null,不過 community_id,post_id 是可能為 null,也可能不為 null
3.初步解法
如以下 SQL 所示,發(fā)現(xiàn)結(jié)果為空,沒有查詢到數(shù)據(jù),實(shí)際是有數(shù)據(jù),問題在于 community_id 和 post_id 對(duì)于空值的處理。
<select id="totalPageInfo" resultType="com.kwan.springbootkwan.entity.dto.CsdnTotalIncomeDTO">
SELECT t1.receiver_nick_name AS nickName
, SUM(t1.received_money) AS amount
FROM table_1 t1 left join table_2 t2
on t1.order_no = t2.order_no
AND t1.community_id=t2.community_id
AND t1.post_id=t2.post_id
WHERE 1 = 1
<if test="query.startDate != null">
AND t1.receive_time <![CDATA[>= #{query.startDate}]]>
AND t2.create_time <![CDATA[>= #{query.startDate}]]>
</if>
<if test="query.endDate != null">
AND t1.receive_time <![CDATA[<= #{query.endDate}]]>
AND t2.create_time <![CDATA[<= #{query.endDate}]]>
</if>
GROUP BY nickName
ORDER BY amount DESC
</select>
二.解決方案
1.SQL 如下.
<select id="totalPageInfo" resultType="com.kwan.springbootkwan.entity.dto.CsdnTotalIncomeDTO">
SELECT t1.receiver_nick_name AS nickName
, SUM(t1.received_money) AS amount
FROM table_1 t1 left join table_2 t2
on t1.order_no = t2.order_no
<![CDATA[
AND t1.community_id <=> t2.community_id
AND t1.post_id<=> t2.post_id
]]>
WHERE 1 = 1
<if test="query.startDate != null">
AND t1.receive_time <![CDATA[>= #{query.startDate}]]>
AND t2.create_time <![CDATA[>= #{query.startDate}]]>
</if>
<if test="query.endDate != null">
AND t1.receive_time <![CDATA[<= #{query.endDate}]]>
AND t2.create_time <![CDATA[<= #{query.endDate}]]>
</if>
GROUP BY nickName
ORDER BY amount DESC
</select>
2.解釋說明
在這個(gè) SQL 查詢中,由于 community_id 和 post_id 可能為空,你可以通過使用 COALESCE 函數(shù)或 IFNULL 函數(shù)(具體取決于你使用的數(shù)據(jù)庫系統(tǒng))來處理可能的空值情況。
下面是一種修改方式,假設(shè)你使用的是 MySQL 數(shù)據(jù)庫:
SELECT
t1.receiver_nick_name AS nickName,
SUM(t1.received_money) AS amount
FROM
table_1 t1
LEFT JOIN
table_2 t2 ON t1.order_no = t2.order_no
AND t1.community_id <=> t2.community_id
AND t1.post_id <=> t2.post_id
WHERE
1 = 1
<if test="query.startDate != null">
AND t1.receive_time <![CDATA[>= #{query.startDate}]]>
AND t2.create_time <![CDATA[>= #{query.startDate}]]>
</if>
<if test="query.endDate != null">
AND t1.receive_time <![CDATA[<= #{query.endDate}]]>
AND t2.create_time <![CDATA[<= #{query.endDate}]]>
</if>
GROUP BY
nickName
ORDER BY
amount DESC
在這里,使用了 <=> 操作符,它在 MySQL 中用于處理 NULL 值的比較。如果 community_id 或 post_id 的其中一個(gè)是 NULL,那么 <=> 操作符會(huì)返回 true。
請根據(jù)你使用的數(shù)據(jù)庫類型來調(diào)整語法。如果是其他數(shù)據(jù)庫,可能會(huì)使用 COALESCE 或 IS NULL 等不同的語法。
到此這篇關(guān)于Mybatis空值關(guān)聯(lián)的問題解析及解決方案的文章就介紹到這了,更多相關(guān)Mybatis空值關(guān)聯(lián)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用SpringBoot集成Thymeleaf和Flying?Saucer實(shí)現(xiàn)PDF導(dǎo)出
在?Spring?Boot?項(xiàng)目中,生成?PDF?報(bào)表或發(fā)票是常見需求,本文將介紹如何使用?Spring?Boot?集成?Thymeleaf?模板引擎和?Flying?Saucer?實(shí)現(xiàn)?PDF?導(dǎo)出,并提供詳細(xì)的代碼實(shí)現(xiàn)和常見問題解決方案,需要的朋友可以參考下2024-11-11
Java實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼的示例代碼
這篇文章主要為大家介紹了如何用Java語言實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼的生成,項(xiàng)目采用了springboot,maven等技術(shù),感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下2022-02-02
SpringBoot實(shí)現(xiàn)動(dòng)態(tài)配置及項(xiàng)目打包部署上線功能
本文講解的是如何使用Spring動(dòng)態(tài)配置文件,實(shí)現(xiàn)不同環(huán)境不同配置,靈活切換配置文件;以及講述了如何使用?Maven?打包,然后上傳至Linux服務(wù)器進(jìn)行部署,對(duì)SpringBoot打包部署上線過程感興趣的朋友一起看看吧2022-10-10
使用FeignClient設(shè)置動(dòng)態(tài)Url
這篇文章主要介紹了使用FeignClient設(shè)置動(dòng)態(tài)Url方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
SpringBoot集成WebSocket的兩種方式(JDK內(nèi)置版和Spring封裝版)
這篇文章主要介紹了SpringBoot集成WebSocket的兩種方式,這兩種方式為JDK內(nèi)置版和Spring封裝版,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
springboot的logging.group日志分組方法源碼流程解析
這篇文章主要為大家介紹了springboot的logging.group日志分組方法源碼流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法
本篇文章主要介紹了Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02

