文檔:https://www.runoob.com/mysql/mysql-join.html
之前的分頁優化寫法(推薦使用 INNER JOIN )
select t1.orderId, t1.venderId, t1.created, t1.modified, t1.pushCreated, t1.pushModified, t1.responseJson
from yd_pop_order t1,
(select orderId from yd_pop_order where venderId = #{venderId} and pushModified between #{startJdpModifyTime} and #{endJdpModifyTime} order by pushModified limit #{offset},#{pageSize}) t2
where t1.orderId = t2.orderId 這種語法 是 MySQL的 什么語法
你提供的查詢使用了MySQL的子查詢(也稱為內查詢或嵌套查詢)和連接(JOIN)操作,但它并沒有顯式地使用JOIN關鍵字,而是使用了老式的、基于逗號分隔的表列表和WHERE子句來進行連接。這種語法在MySQL中是合法的,但通常建議使用更現代的JOIN語法,因為它更清晰、更易于閱讀和維護。
這里是你的查詢的解析:
-
子查詢 (
t2):(select orderId from yd_pop_order where venderId = #{venderId} and pushModified between #{startJdpModifyTime} and #{endJdpModifyTime} order by pushModified limit #{offset},#{pageSize}) t2這個子查詢從
yd_pop_order表中選擇了滿足特定條件的orderId。它過濾了venderId匹配給定值,并且pushModified在指定時間范圍內的記錄。然后,它按照pushModified排序,并使用LIMIT子句來限制結果集的大小,實現分頁。 -
主查詢:
select t1.orderId, t1.venderId, t1.created, t1.modified, t1.pushCreated, t1.pushModified, t1.responseJson from yd_pop_order t1, ... where t1.orderId = t2.orderId主查詢從
yd_pop_order表中選擇了一系列列,并且與子查詢的結果進行了連接。連接條件是t1.orderId = t2.orderId,這意味著它只選擇那些在主表中其orderId也出現在子查詢結果中的記錄。 -
連接(JOIN):
盡管這個查詢沒有使用JOIN關鍵字,但它實際上執行了一個連接操作。在MySQL中,你可以通過逗號分隔表名并在WHERE子句中指定連接條件來實現連接。這種語法是合法的,但通常推薦使用INNER JOIN、LEFT JOIN等關鍵字來明確連接的類型,因為這樣做更清晰。
為了使用更現代的語法,你可以將查詢重寫為:
SELECT t1.orderId, t1.venderId, t1.created, t1.modified, t1.pushCreated, t1.pushModified, t1.responseJson
FROM yd_pop_order t1
INNER JOIN (
SELECT orderId
FROM yd_pop_order
WHERE venderId = #{venderId}
AND pushModified BETWEEN #{startJdpModifyTime} AND #{endJdpModifyTime}
ORDER BY pushModified
LIMIT #{offset}, #{pageSize}
) t2 ON t1.orderId = t2.orderId;
在這個重寫中,我使用了INNER JOIN來明確地表示連接操作。這樣,查詢的意圖就更加清晰了。


本文來自博客園,作者:del88,轉載請注明原文鏈接:http://www.rzrgm.cn/del88/p/18524715
浙公網安備 33010602011771號