sql 判断选择语句

1 选择判断

select case 2 when 1 then 'one' when 2 then 'two' else 'more' end union all select case 2 when 1 then 'one' when 2 then 'two' else 'more' end;

2 判断 有点像三元运算符

select case when 1>2 then 'true' else 'false' end;

3 插入和更新二选一

INSERT INTO "fd_question_answer_2WIQRCZAPA" ( ID, uid, title )
VALUES
    ( 21, '9675adb618aab1264', '这是id为2的回答' ) ON conflict ( ID ) DO
UPDATE 
    SET uid = 'postgreOk',
    title = '一次成功了';

如果id为21的存在则实现更新操作,否则实现插入数据, 前提是on conflict ( x)  x在数据中需是唯一约束  

原文地址:https://www.cnblogs.com/spring20190213dream/p/10580937.html