数据库
首页 > 数据库> > sql_上移_下移_置顶_置底

sql_上移_下移_置顶_置底

作者:互联网


    <!--置顶-->
    <update id="moveTop" parameterType="com.hisign.oses.entity.os.Jcybxx">
        UPDATE
        <include refid="TableClause"/>
        SET XH = (CASE WHEN XH = #{xh} THEN 1 ELSE XH + 1 END)
        WHERE
        instr(
        (SELECT if(rn = 1, NULL, bhs) FROM (
        SELECT group_concat(BH) AS bhs, count(*) AS rn FROM (
        SELECT BH FROM
        <include refid="TableClause"/>
        WHERE
        WTZJBH = #{wtzjbh} AND XH <![CDATA[  <= ]]> #{xh} AND SCBJ = 0) t) t), BH) <![CDATA[  > ]]> 0
    </update>

    <!--上移-->
    <update id="moveUp" parameterType="com.hisign.oses.entity.os.Jcybxx">
        UPDATE
        <include refid="TableClause"/>
        SET XH = (CASE WHEN XH = #{xh} THEN #{xh} - 1 ELSE #{xh} END)
        WHERE
        instr(
        (SELECT if(rn = 2, bhs, NULL) FROM (
        SELECT group_concat(BH) AS bhs, count(*) AS rn FROM (
        SELECT BH FROM
        <include refid="TableClause"/>
        WHERE
        WTZJBH = #{wtzjbh} AND XH <![CDATA[  <= ]]> #{xh} AND SCBJ = 0 ORDER BY XH DESC LIMIT 2 ) t) t), BH) <![CDATA[  > ]]> 0
    </update>

    <!--下移-->
    <update id="moveDown" parameterType="com.hisign.oses.entity.os.Jcybxx">
        UPDATE
        <include refid="TableClause"/>
        SET XH = (CASE WHEN XH = #{xh} THEN #{xh} + 1 ELSE #{xh} END)
        WHERE
	    instr(
        (SELECT if(rn = 2, bhs, NULL) FROM (
		SELECT group_concat(BH) AS bhs, count(*) AS rn FROM (
		SELECT BH FROM
        <include refid="TableClause"/>
		WHERE
        WTZJBH = #{wtzjbh} AND XH <![CDATA[  >= ]]> #{xh} AND SCBJ = 0 ORDER BY XH LIMIT 2 ) t) t), BH) <![CDATA[  > ]]> 0
    </update>

    <!--置底-->
    <update id="moveBottom" parameterType="com.hisign.oses.entity.os.Jcybxx">
        UPDATE
        <include refid="TableClause"/>
        SET XH = (CASE WHEN XH = #{xh} THEN @maxord ELSE XH - 1 END)
        WHERE
        instr(
        (SELECT if(rn = 1, NULL, bhs) FROM (
        SELECT group_concat(BH) as bhs, count(*) AS rn, (SELECT @maxord:= (SELECT MAX(XH))) AS maxord FROM
        (SELECT BH,XH FROM
        <include refid="TableClause"/>
        WHERE WTZJBH = #{wtzjbh} AND XH <![CDATA[  >= ]]> #{xh} AND SCBJ = 0) t) t),BH) <![CDATA[  > ]]> 0
    </update>

标签:xh,rn,BH,XH,置顶,sql,bhs,置底,SELECT
来源: https://blog.csdn.net/weixin_46649054/article/details/120744891