JAVA使用left on连接多表,存错,使用where就没有问题

使用left on保存,请问如何结局

<select id="selectByUserAndStatusV2" resultType="cn.spring.inter.entity.CodeTask">
        SELECT
        <include refid="Base_Column_List" />,
        td.explanation AS taskDifficulty,
        cc.name AS taskType
        FROM code_task ct
--         task_status ts,
--         task_difficulty td
        LEFT JOIN task_status ts on ct.status= ts.id
        LEFT JOIN task_difficulty td on ct.difficulty = td.id
        LEFT JOIN cad_classify cc ON ct.type = cc.id
        <where>
--             AND ct.status = ts.id
--             AND IFNULL(ct.difficulty, 0) = td.id
--             AND IFNULL(ct.type, 0) = cc.id
            <if test="userID != null">
                AND ct.userID = #{userID}
            </if>
            <if test="flagDelete != null">
                AND flag_delete = #{flagDelete}
            </if>
            <if test="statusList!=null and statusList.size()>0">
                AND ct.status in
                <foreach collection="statusList" item="item" index="index" open="(" separator="," close=")">
                    replace('${item}',' ','')
                </foreach>
            </if>
        </where>
        <choose>
            <when test="sort=='ASC'">
                order by id ASC limit #{start}, #{dataLength}
            </when>
            <otherwise>
                order by id DESC limit #{start}, #{dataLength}
            </otherwise>
        </choose>
    </select>
阅读 1.6k
1 个回答

使用<where>标签mybatis会帮你去掉AND ct.userID = 2前的AND,如果只是用了sql的where,不会帮你去掉,生成的sql为 select .... from table where AND ct.userID = 2 ,所以报sql语法问题。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题