只有 id=9223 level=1 两个参数把执行的SQL发我(不要改变原有SQL只是替换参数,最终MySQL可执行的SQL发我) <select id="abnormalFactors" resultType="com.bsh.ih.company.entity.CompRosterHra3EvalBj"> select count(1) as totalPerson, sum(crhe.group_bp) as groupBp, sum(crhe.group_bg) as groupBg, sum(crhe.group_hcy) as groupHcy, sum(crhe.group_bf) as groupBf, sum(crhe.group_weight) as groupWeight, sum(crhe.group_ua) as groupUa, sum(crhe.group_other_high_risk) as groupOtherHighRisk from comp_roster_hra3_eval crhe JOIN ( SELECT import_roster_id, MAX(report_time) as report_time FROM comp_roster_hra3_eval GROUP BY import_roster_id ) m ON crhe.import_roster_id = m.import_roster_id AND crhe.report_time = m.report_time left join comp_import_roster cir on crhe.import_roster_id = cir.id <where> <if test="level == 1"> and cir.ent_org_2 in (select id from comp_org_info where pid = #{id}) </if> <if test="level == 2"> and cir.ent_org_2 = #{id} </if> <if test="level == 3"> and cir.ent_org_3 = #{id} </if> <if test="gender != null and gender != ''"> and cir.gender = #{gender} </if> <if test="type == 1"> and TIMESTAMPDIFF(YEAR, cir.birthday, CURDATE()) < 35 </if> <if test="type == 2"> and TIMESTAMPDIFF(YEAR, cir.birthday, CURDATE()) >= 35 and TIMESTAMPDIFF(YEAR, cir.birthday, CURDATE()) <= 50 </if> <if test="type == 3"> and TIMESTAMPDIFF(YEAR, cir.birthday, CURDATE()) > 50 </if> </where> </select>
时间: 2025-06-09 18:54:51 浏览: 5
<think>好的,我现在需要处理用户的SQL查询请求。用户给了两个参数,id=9223和level=1,要求生成最终的MySQL可执行SQL,不能改变原有结构,只是替换参数。首先,我得仔细看看用户提供的SQL模板,特别是where条件部分。
首先,原SQL中的where条件里有几个<if>标签,根据不同的参数值来动态添加条件。现在用户指定的level=1,所以应该触发第一个<if>条件,也就是level==1时的部分。这里会添加一个子查询,检查cir.ent_org_2是否在某个子查询的结果中。而子查询的pid参数会被替换为id=9223的值。
接下来,我需要确认其他条件是否有参数传入。用户没有提到gender和type参数,所以这些条件不会被包含在最终的SQL中。这时候,其他<if>标签的内容会被忽略,不会出现在生成的SQL里。
然后,我需要确保原有的SQL结构不被改变,只是替换参数。这里要注意的是,原SQL中的<where>标签会自动处理条件前的AND,如果所有条件都不满足,where关键字也不会出现。但在这个案例中,因为level=1存在,所以会有一个AND条件,但where标签会自动去掉开头的AND,所以最终的条件应该是WHERE cir.ent_org_2 in (...)。
接下来,替换参数的位置。在MyBatis中,#{id}会被替换成具体的值,这里id=9223,所以子查询中的pid=#{id}会变成pid=9223。同样,level==1的条件已经被满足,所以其他level相关的条件不会出现。
最后,检查整个SQL是否符合MySQL的语法,特别是子查询和连接部分是否正确。确保所有的表别名和列名都正确无误,并且连接条件正确。例如,comp_roster_hra3_eval表别名为crhe,与子查询m连接,再左连接comp_import_roster表cir。这些部分都需要保持原样。
总结一下,最终的SQL应该包含level=1对应的条件,替换id为9223,其他条件因为没有参数传入而被省略。生成的SQL需要正确拼接,确保语法正确,并且能够直接在MySQL中执行。</think>根据参数 id=9223 和 level=1,生成的最终可执行 MySQL SQL 如下:
```sql
SELECT
COUNT(1) AS totalPerson,
SUM(crhe.group_bp) AS groupBp,
SUM(crhe.group_bg) AS groupBg,
SUM(crhe.group_hcy) AS groupHcy,
SUM(crhe.group_bf) AS groupBf,
SUM(crhe.group_weight) AS groupWeight,
SUM(crhe.group_ua) AS groupUa,
SUM(crhe.group_other_high_risk) AS groupOtherHighRisk
FROM
comp_roster_hra3_eval crhe
JOIN (
SELECT
import_roster_id,
MAX(report_time) AS report_time
FROM
comp_roster_hra3_eval
GROUP BY
import_roster_id
) m
ON crhe.import_roster_id = m.import_roster_id
AND crhe.report_time = m.report_time
LEFT JOIN comp_import_roster cir
ON crhe.import_roster_id = cir.id
WHERE
cir.ent_org_2 IN (
SELECT
id
FROM
comp_org_info
WHERE
pid = 9223
);
```
参数替换说明:
1. `level == 1` 条件被激活,使用 `ent_org_2 IN` 子查询
2. 子查询中的 `#{id}` 被替换为 9223
3. 由于未提供 gender 和 type 参数,相关条件被自动过滤
4. 保持原有 SQL 结构和计算逻辑不变
阅读全文
相关推荐

















