refactor: 更改子查询为join

This commit is contained in:
张松 2024-12-30 20:15:43 +08:00
parent 940f41930f
commit c4852d5e7e
1 changed files with 140 additions and 137 deletions

View File

@ -8,66 +8,68 @@
</update> </update>
<select id="selectCourse" resultType="Map"> <select id="selectCourse" resultType="Map">
SELECT SELECT
c.classify_id AS classifyId, c.classify_id as classifyId,
c.course_id AS courseId, c.course_id as courseId,
c.course_label AS courseLabel, c.course_label as courseLabel,
c.create_time AS createTime, c.create_time createTime,
c.details, c.details,
c.img, c.img,
c.banner_img AS bannerImg, c.banner_img as bannerImg,
c.status, c.status,
c.is_delete AS isDelete, c.is_delete as isDelete,
c.msg_type AS msgType, c.msg_type msgType,
c.msg_url AS msgUrl, c.msg_url as msgUrl,
c.pay_num AS payNum, c.pay_num as payNum,
c.price, c.price,
c.wholesale_price AS wholesalePrice, c.wholesale_price as wholesalePrice,
c.is_over AS isOver, c.is_over as isOver,
c.title, c.title,
c.is_price AS isPrice, c.is_price as isPrice,
c.title_img AS titleImg, c.title_img as titleImg,
c.update_time AS updateTime, c.update_time as updateTime,
cc.classification_name AS classificationName, cc.classification_name as classificationName,
c.course_type AS courseType, c.course_type as courseType,
c.banner_id AS bannerId, c.banner_id as bannerId,
b.name AS bannerName, b.name as bannerName,
0 AS viewCounts, 0 as viewCounts,
c.dy_img_id AS dyImgId, c.dy_img_id as dyImgId,
c.dy_course_id AS dyCourseId, c.dy_course_id as dyCourseId,
c.wx_course_id AS wxCourseId, c.wx_course_id as wxCourseId,
c.wx_show AS wxShow, c.wx_show as wxShow,
c.dy_show AS dyShow, c.dy_show as dyShow,
c.sort, c.sort,
ROUND(10000 * RAND(), 0) AS goodNum, round(10000*rand(),0) as goodNum,
d.courseDetailsCount, cd.course_details_count as courseDetailsCount, -- Replaced subquery with left join count
NULL AS courseDetailsName, null AS courseDetailsName,
NULL AS courseDetailsId, null AS courseDetailsId,
ROUND(10000 * RAND(), 0) AS weekGoodNum, round(10000*rand(),0) as weekGoodNum,
0 AS isRecommend 0 as isRecommend
FROM FROM
course AS c course AS c
LEFT JOIN LEFT JOIN course_classification AS cc ON c.classify_id = cc.classification_id
course_classification AS cc ON c.classify_id = cc.classification_id LEFT JOIN banner AS b ON b.id = c.banner_id
LEFT JOIN LEFT JOIN (
banner AS b ON b.id = c.banner_id SELECT
LEFT JOIN course_id,
(SELECT course_id, COUNT(*) AS courseDetailsCount COUNT(*) as course_details_count
FROM course_details FROM
GROUP BY course_id) AS d ON c.course_id = d.course_id course_details
LEFT JOIN GROUP BY
(SELECT course_id FROM course_details WHERE good = 1) AS gd ON c.course_id = gd.course_id course_id
) AS cd ON c.course_id = cd.course_id
WHERE WHERE
c.is_delete = 0 1 = 1
<if test='null != wxShow and wxShow == 1'> AND c.is_delete = 0
<if test='null != wxShow and wxShow==1'>
AND c.wx_show = #{wxShow} AND c.wx_show = #{wxShow}
</if> </if>
<if test='null != wxShow and wxShow == 2'> <if test='null != wxShow and wxShow==2'>
AND (c.wx_show = #{wxShow} OR c.wx_show IS NULL) AND (c.wx_show = #{wxShow} OR c.wx_show IS NULL)
</if> </if>
<if test='null != dyShow and dyShow == 1'> <if test='null != dyShow and dyShow==1'>
AND c.dy_show = #{dyShow} AND c.dy_show = #{dyShow}
</if> </if>
<if test='null != dyShow and dyShow == 2'> <if test='null != dyShow and dyShow==2'>
AND (c.dy_show = #{dyShow} OR c.dy_show IS NULL) AND (c.dy_show = #{dyShow} OR c.dy_show IS NULL)
</if> </if>
<if test='null != title'> <if test='null != title'>
@ -86,7 +88,7 @@
AND c.is_over = #{over} AND c.is_over = #{over}
</if> </if>
<if test="isRecommend != null and isRecommend != -1"> <if test="isRecommend != null and isRecommend != -1">
AND gd.course_id IS NOT NULL AND c.course_id IN (SELECT course_id FROM course_details WHERE good = 1)
</if> </if>
<if test="status != null and status != 0"> <if test="status != null and status != 0">
AND c.status = #{status} AND c.status = #{status}
@ -108,10 +110,11 @@
</if> </if>
<if test="sort != null and sort == 2"> <if test="sort != null and sort == 2">
ORDER BY weekGoodNum DESC ORDER BY weekGoodNum DESC
</if>; </if>
<!--
<!--
SELECT SELECT
c.classify_id as classifyId, c.classify_id as classifyId,
c.course_id as courseId, c.course_id as courseId,