如何使用 IF TEST 語句在 SQL 中判斷字段是否在列表中?

如何使用 IF TEST 語句在 SQL 中判斷字段是否在列表中?

sql 中使用 if test 判斷字段是否在列表中

在場(chǎng)景中,需要進(jìn)行批量更新,其中變量 fieldnameslist 儲(chǔ)存了需要更新的字段名。為了動(dòng)態(tài)更新特定字段,需要使用 sql 中的 if test 語句來判斷字段是否在列表中。

修改后的 xml 代碼如下:

<update id="batchUpdateById">     <foreach collection="entityList" item="entity" index="index1" open="(" close=")" separator=";">         UPDATE school_info SET         <if test='fieldNamesList.contains("schoolNo")'>             schoolNo = #{entity.getSchoolNo}         </if>         <if test='fieldNamesList.contains("schoolRank")'>             schoolRank = #{entity.getSchoolRank}         </if>         where dataId = #{entity.dataId}     </foreach> </update>

該代碼使用 if test 語句判斷 fieldnameslist 是否包含特定的字段名,如果是,則更新該字段為實(shí)體對(duì)象中相應(yīng)的值。這樣就可以根據(jù) fieldnameslist 動(dòng)態(tài)更新指定的字段了。

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊6 分享