select
p.pr_id, p.depot_id, p.depot_name, p.manager_user_id, p.manager_user_name,p.brand_id,p.brand_name,p.desc_id,p.desc_name,p.model_id,p.model_name,p.standard_repertory,
p.prewarning_value,count(case when s.quality_id=1 then 1 end) as real_repertory,p.remark,p.create_emp,p.create_tm,p.update_emp,p.update_tm,p.apply_tm,p.notice_value, p.standard_mean_value, p.standard_adjust
FROM
`table1` p
LEFT JOIN
(select desc_id,model_id,brand_id,quality_id,depot_id from table2 where sp_status_id in (1,2,8)) s
ON p.depot_id = s.depot_id
and p.desc_id = s.desc_id
and s.model_id in (SELECT g.model_id from table3 g where g.general_code=(SELECT o.general_code FROM table3 o where o.model_id=p.model_id))
where 1=1
<if test="depotId != null">
and p.depot_id = #{depotId, jdbcType=INTEGER}
</if>
<if test="managerUserId != null">
and p.manager_user_id = #{managerUserId, jdbcType=INTEGER}
</if>
<if test="descId != null">
and p.desc_id = #{descId, jdbcType=INTEGER}
</if>
<if test="modelId != null">
and p.model_id = #{modelId, jdbcType=INTEGER}
</if>
<if test="brandId != null">
and p.brand_id = #{brandId, jdbcType=INTEGER}
</if>
<if test="managerUserName != null and managerUserName != ''">
and p.manager_user_name = #{managerUserName, jdbcType=VARCHAR}
</if>
<if test="startTime != null">
and p.apply_tm >= #{startTime, jdbcType=TIMESTAMP}
</if>
<if test="endTime != null">
and p.apply_tm <= #{endTime, jdbcType=TIMESTAMP}
</if>
GROUP by p.pr_id having 1=1
<if test="noticeType != null and noticeType ==1">
and real_repertory <= p.prewarning_value
</if>
<if test="realRepertory != null">
and real_repertory=#{realRepertory, jdbcType=INTEGER}
</if>
上一任开发者写了一个 sql 语句,我想优化一下,但是发现自己能力有限,谁可以帮个忙;
说说情况:
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.