zoukankan      html  css  js  c++  java
  • postgres if ,when及判断表是否存在的sql编写

    判断表是否存在方法1:

    复制代码
    SELECT  case  WHEN   a.cc =1 THEN 2 else 0 END
    FROM
    (
    select  count(*) as cc from pg_class where relname = 'wo'    --wo is table name,pg_class是pg自带的关键字
    ) as a  
    复制代码

    方法2:

    用if判断,但有以下条件

    The IF statement is part of the default procedural language PL/pgSQL. You need to create a function or execute an ad-hoc statement with the DO command.、

    exist使用要求和if一样

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    DO
    $do$
    BEGIN
    IF (select  count(*) cc from pg_class where relname = 'wo') = 0 --一定要加括号
    THEN
       INSERT INTO wo VALUES (2);
    ELSE
       INSERT INTO wo VALUES (1);
    END IF;
    END
    $do$

     联合使用select 和insert的方法如下

    create table language_ko(Key text,XML text);
    insert into Language_ko(key,xml) (select "Key " as key,'<'||"Key"||'>'||"Korean"||'</'||"Key"||'>' as xml  from "centralClient1")
  • 相关阅读:
    史上最强验证
    Yii2 return redirect()
    一次线上问题引发的思考
    一次前端体验优化
    RSA For PHP
    判断是否字符串是否是JSON
    过滤Xss
    Yii2 中日志的记录
    Yii2 中禁用csrf校验
    开始。
  • 原文地址:https://www.cnblogs.com/xr210/p/12727941.html
Copyright © 2011-2022 走看看