工作中常见sql问题

1 批量操作

  <insert id="batchInsert" parameterType="java.util.ArrayList">
  	insert into 
  		gis2_msg_info_ext(msg_id, tag_name, tag_value)
  	values
  	<foreach item="sms" collection="list" separator="," >
  		(#{sms.msgId,jdbcType=BIGINT},
		 #{sms.tagName,jdbcType=VARCHAR},
		 #{sms.tagValue,jdbcType=VARCHAR})
  	</foreach>
  </insert>

批量匹配:
<if test="status!=null and status.size>0">
    and a.status in
<foreach collection="status" index="index" item="state" separator="," open="(" close=")">
     #{state}
</foreach>
</if>

2  强制转换

1 city_id = #{cityId}::numeric

2 select 
  COALESCE(sum(sendcount::NUMERIC),0) as saleCount  from  gis2_msg_info

3 on a.channel_id=CAST(b.id as varchar)

4 cast(a.ACTIVE_CURR_NUMS*100.00/b.ACTIVE_CURR_NUMS  as numeric(16,2)) 

3       &lt;= 
        &gt;=

        append("//n")字符串中换行

4 时间条件

update 
   gis2_msg_info
set 
     edit_time=to_timestamp(''||now()||'','YYYY-MM-DD HH24:MI:SS')::timestamp,	 
and	to_date(to_char(a.edit_time, 'YYYY-MM-DD'),'YYYY-MM-DD') = to_date(#{editDate},'YYYY-MM-DD') 
and a.edit_time &lt;= to_date(#{endDate},'YYYY-MM-DD HH24:MI:SS')

and a.msg_content like CONCAT(CONCAT('%', #{msgContent}), '%') 

5 查询表名,序列

查询最新表 : select TABLE_NAME from information_schema.TABLES where TABLE_NAME like 
  'dw_antiracketeering_detail%' order by table_name DESC limit 1

是否存在表 : select 1 from information_schema.tables  where  table_schema = 'public' 
 and  table_name = 'dw_antiracketeering_detail_201808'
  
表得序列: create sequence  tbl_serial_a_seq    increment by 1 minvalue 1 no maxvalue start with 1;
查询序列: select nextval('tbl_serial_a_seq');
 

猜你喜欢

转载自blog.csdn.net/zhilinboke/article/details/82907621