使用if构建Mybatis查询

时间:2014-09-22 15:17:54

标签: java sql oracle mybatis

我写了下一个代码。我知道这是错的,但我不知道应该如何做同样的事情。我会感激任何帮助。

<select id="getSeqTempByRulesCabN3" resultType="java.lang.Integer" >
    select templat.seq_temp
    from re_t_vp_voucher_template templat, re_t_vp_voucher_rules rules_t, re_t_vp_voucher_rules_pais pais
    where templat.seq_temp = rules_t.seq_temp
                and rules_t.SEQ_RULE = PAIS.SEQ_RULE
    <if test="(select count(*) as cuenta 
        from re_t_vp_voucher_template templat, re_t_vp_voucher_rules rules_t, re_t_vp_voucher_rules_pais pais
        where templat.seq_temp = rules_t.seq_temp
                and rules_t.SEQ_RULE = PAIS.SEQ_RULE
                AND rules_t.seq_rec is not null
                and rules_t.seq_rec = #{seqRec, jdbcType=DECIMAL}
        group by templat.seq_temp,  rules_t.fec_desde_res, rules_t.cod_idioma) != 0">
                and rules_t.seq_rec = #{seqRec, jdbcType=DECIMAL}
    </if>
                and rules_t.seq_ttoo = #{seqTtoo, jdbcType=DECIMAL}
    group by templat.seq_temp,  rules_t.fec_desde_res, rules_t.cod_idioma
</select>

2 个答案:

答案 0 :(得分:0)

确定。我已经知道它为什么不起作用了。您不能简单地在测试标记内编写完整的sql select查询。测试标签仅接受参数。实际上,我将select的响应作为参数传递。

答案 1 :(得分:0)

我有一个可怕的解决方案。如果你的查询工作得足够快,也许这可行。

您的条件为if a!=0 then b,可以表示为( (a!=0 AND b) OR (a=0) )

因此,我提供的是:

SELECT templat.seq_temp
FROM re_t_vp_voucher_template templat,
     re_t_vp_voucher_rules rules_t,
     re_t_vp_voucher_rules_pais pais
WHERE templat.seq_temp = rules_t.seq_temp
  AND rules_t.SEQ_RULE = PAIS.SEQ_RULE
  AND rules_t.seq_ttoo = #{seqTtoo, jdbcType=DECIMAL}
  AND ((rules_t.seq_rec = #{seqRec, jdbcType=DECIMAL}
        AND
          (SELECT count(*) AS cuenta
           FROM re_t_vp_voucher_template templat,
                re_t_vp_voucher_rules rules_t,
                re_t_vp_voucher_rules_pais pais
           WHERE templat.seq_temp = rules_t.seq_temp
             AND rules_t.SEQ_RULE = PAIS.SEQ_RULE
             AND rules_t.seq_rec IS NOT NULL
             AND rules_t.seq_rec = #{seqRec, jdbcType=DECIMAL}
           GROUP BY templat.seq_temp,
                    rules_t.fec_desde_res,
                    rules_t.cod_idioma) != 0)
       OR
         (SELECT count(*) AS cuenta
          FROM re_t_vp_voucher_template templat,
               re_t_vp_voucher_rules rules_t,
               re_t_vp_voucher_rules_pais pais
          WHERE templat.seq_temp = rules_t.seq_temp
            AND rules_t.SEQ_RULE = PAIS.SEQ_RULE
            AND rules_t.seq_rec IS NOT NULL
            AND rules_t.seq_rec = #{seqRec, jdbcType=DECIMAL}
          GROUP BY templat.seq_temp,
                   rules_t.fec_desde_res,
                   rules_t.cod_idioma) = 0)
GROUP BY templat.seq_temp,
         rules_t.fec_desde_res,
         rules_t.cod_idioma