数据库问题:
sql server数据库中的 if exists()  使用起来很方便  可是在PostgreSQL 中是没有这种用法的。
一个数据语句想要实现:表中存在这个数据则update  不存在 则 insert into
create table userInfo(
userId char(),
userName char())
我们先向表中插入一条数据  便于接下来的演示
insert into userInfo (userId,userName) values ('test','张三');
SQL server实现 :很简单,使用 if exists()就可以了
if exists(select * fron userInfo where userName = '张三')
update userInfo set userId = 'zhangsan' where userName = '张三'
else 
insert into userInfo(userId,userName) values ('zhangsan','张三')
语句实现的效果为:判断表中  username为是否存在'张三',若存在在更新其userId为'zhangsan',若不在 则插入数据

在postgreSQL中因为没有 if exist ()  怎么实现呢?
我在看SQL的时候  看到了count()这个SQl函数,这个函数返回指定列的值的数目。
什么意思呢?我用一条sql语句  就可以看明白它的意思了
select count(userName) from userInfo where userName = '张三';
这句sql语句的执行结果 为 count 1;
然后我又测试了一条语句:
 select count(userName) from userInfo where userName = '王五'; 
执行结果为:count 0;
由此我得出结论  可以使用count()来判断 某个表中是否存在某个特定的字段,然后根据返回值的不同来执行不同的sql语句。
select(userName) as test from userInfo where userName = '张三';
然后对test的值进行判断
if(test>0)
{
update userInfo set userId = 'zhangsan' where userName = '张三'
}
else
insert into userInfo(userId,userName) values ('zhangsan','张三')
这样就可以实现if exists()的效果