mybatis的SQL语句求和是根据多个条时where怎么写?

SQL语句where查询多个条件,比如运输ID是2的路程类型是去程的已经支付的费用怎么写SQL语句,并且ID是当做参数传入的

img

img

把你的条件部分模仿这样写:

       <where>
          <trim prefix="" suffix="" suffixOverrides="and" >
          
              <if test="keywords != null" >
                     EN_NAME like  CONCAT('%','#{keywords,jdbcType=VARCHAR}','%' ) and 
              </if>
              
              <if test="userId != null and recommend ==null">
                     USER_ID = #{userId,jdbcType=VARCHAR}   and
              </if>
              
              <if test="classId != null">
                     CLASS_ID = #{classId,jdbcType=VARCHAR}   and
              </if>

          </trim>
      </where>

where标签后面不要直接跟and,你可以把if标签后面的条件放在前面,这样就不会出现where后面直接跟and了

你要的是这个吧,把这几个数据一个sql查出来。

xml

<select id="sumBisTransDetail" resultType="xx.xx.xx.SumBisTransDetail">
select sum( if(type=2,amount,0)) as fcTypeAmount ,
sum( if(zf_state=1,amount,0)) as payAmount,
sum( if(zf_state=1 and type=1,amount,0)) as qcZfTypeAmount,
sum( if(zf_state=1 and type=2,amount,0)) as fcZfTypeAmount,
from bis_trans_detail
<where>
<if test="transId != null">
 and trans_id = #{transId}
</if>
</ where>
</select>

xx.xx.xx是你SumBisTransDetail.java类的包路径,根据实际情况填写哈。

SumBisTransDetail.java

package xx.xx.xx;

import java.math.BigDecimal;

public class SumBisTransDetail {
    private BigDecimal fcTypeAmount;
    private BigDecimal payAmount;
    private BigDecimal qcZfTypeAmount;
    private BigDecimal fcZfTypeAmount;

    public BigDecimal getFcTypeAmount() {
        return fcTypeAmount;
    }

    public void setFcTypeAmount(BigDecimal fcTypeAmount) {
        this.fcTypeAmount = fcTypeAmount;
    }

    public BigDecimal getPayAmount() {
        return payAmount;
    }

    public void setPayAmount(BigDecimal payAmount) {
        this.payAmount = payAmount;
    }

    public BigDecimal getQcZfTypeAmount() {
        return qcZfTypeAmount;
    }

    public void setQcZfTypeAmount(BigDecimal qcZfTypeAmount) {
        this.qcZfTypeAmount = qcZfTypeAmount;
    }

    public BigDecimal getFcZfTypeAmount() {
        return fcZfTypeAmount;
    }

    public void setFcZfTypeAmount(BigDecimal fcZfTypeAmount) {
        this.fcZfTypeAmount = fcZfTypeAmount;
    }
}