Mybatis空值關(guān)聯(lián)的問題解析及解決方案
一.問題描述
1.已知條件
已知 table_1 有 3 個字段 order_no,community_id,post_id
已知 table_2 也有 3 個字段 order_no,community_id,post_id
2.關(guān)聯(lián)條件
現(xiàn)在需要將 table_1 和 table_2 進行關(guān)聯(lián),關(guān)聯(lián)條件是 order_no,community_id,post_id 這 3 個字段,但是 order_no 不為 null,不過 community_id,post_id 是可能為 null,也可能不為 null
3.初步解法
如以下 SQL 所示,發(fā)現(xiàn)結(jié)果為空,沒有查詢到數(shù)據(jù),實際是有數(shù)據(jù),問題在于 community_id 和 post_id 對于空值的處理。
<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.解釋說明
在這個 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 的其中一個是 NULL,那么 <=> 操作符會返回 true。
請根據(jù)你使用的數(shù)據(jù)庫類型來調(diào)整語法。如果是其他數(shù)據(jù)庫,可能會使用 COALESCE 或 IS NULL 等不同的語法。
到此這篇關(guān)于Mybatis空值關(guān)聯(lián)的問題解析及解決方案的文章就介紹到這了,更多相關(guān)Mybatis空值關(guān)聯(lián)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring?boot項目實戰(zhàn)之實現(xiàn)與數(shù)據(jù)庫的連接
在我們?nèi)粘5拈_發(fā)過程中,肯定不可避免的會使用到數(shù)據(jù)庫以及SQL?語句,下面這篇文章主要給大家介紹了關(guān)于spring?boot項目實戰(zhàn)之實現(xiàn)與數(shù)據(jù)庫連接的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-05-05
SpringBoot框架實現(xiàn)支付和轉(zhuǎn)賬功能
在 Spring Boot 框架中實現(xiàn)支付和轉(zhuǎn)賬功能時,涉及到多個細節(jié)和注意點,這些功能通常需要高度的安全性、穩(wěn)定性和可擴展性,本文介紹了實現(xiàn)支付和轉(zhuǎn)賬功能的一些關(guān)鍵點,需要的朋友可以參考下2024-08-08
SpringBoot獲取ApplicationContext的3種方式
這篇文章主要為大家詳細介紹了SpringBoot獲取ApplicationContext的3種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09
Java Flink窗口觸發(fā)器Trigger的用法詳解
Trigger(窗口觸發(fā)器)決定了窗口(由 WindowAssigner 產(chǎn)生)什么時候調(diào)用窗口處理函數(shù)。可以根據(jù)指定的時間或數(shù)據(jù)元素條件來決定什么時候觸發(fā)。本文將詳細講講其用法,需要的可以參考一下2022-07-07
Java響應式編程之Flux與SseEmitter深度解析(附詳細代碼)
這篇文章主要介紹了Java響應式編程之Flux與SseEmitter深度解析的相關(guān)資料,從需求分析、技術(shù)背景、使用方法、底層原理、性能對比到生產(chǎn)環(huán)境實戰(zhàn),全面解析了這兩種技術(shù)的特點、應用場景及優(yōu)缺點,需要的朋友可以參考下2026-01-01

