首页 > PostgreSQL insert 前判断是否存在

PostgreSQL insert 前判断是否存在

表结构 id,tag_id,player_id
现在有 player_id 的 list 和 tag_id 的 list
在插入前想判断 tag_id,player_id 的组合是否存在,如果存在不插入

试了很多 insert select 。都没能满足


新的Postgresql 9.5总算支持upsert了。

如果条件所限不能用最新版本,就只能考虑用触发器实现类似的功能,google能找到,关键字就是postgresql加upsert,还是挺复杂的。


不用9.5也可以解决。。

insert into table1(col1) select 'value1' from table1 where not exists (select 1 from table1 where col1 = 'value1')

【热门文章】
【热门文章】