1. 程式人生 > 實用技巧 >記錄學習中用到的sql語句

記錄學習中用到的sql語句

1、根據一張表更新另一張表的資料

update t_table t1
set a = t2.a,
	b = t2.b
from t_table t2
where t1.name=t2.name

2、批量更新

mybatis批量更新

<update id="updateBatch" parameterType="java.util.List">
        update mydata_table
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="status =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                     when id=#{item.id} then #{item.status}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item.id,jdbcType=BIGINT}
        </foreach>
    </update>

對應sql語句:

update mydata_table 
    set status = 
    case
        when id = #{item.id} then #{item.status}//此處應該是<foreach>展開值
    end
    where id in (...);

3、pgsql時間戳查詢

select extract(epoch FROM now())

4、SQL高效查詢兩個表不同的資料

select * from B where (select count(1) from A where A.ID = B.ID) = 0