PGsql 查表字段名,字段类型

//判断表中是否存在该字段
   SELECT count(1)
   FROM (
     //获取字段名方法一
     SELECT attname 
     FROM pg_catalog.pg_attribute 
     WHERE attstattarget = - 1 
	 AND attrelid IN ( SELECT oid FROM pg_catalog.pg_class WHERE relname = '表名')
     //方法二
     //select * from  information_schema.columns where table_name = '表名' and column_name = '字段名'
    ) t
    where attname='字段名'

    //获取字段类型
    select t."type" 
    from (
       SELECT format_type ( a.atttypid, a.atttypmod ) AS type,
       a.attname AS fname
       FROM pg_class AS c,pg_attribute AS a
       WHERE c.relname = #{tableName}
       AND a.attrelid = c.oid AND a.attnum >0
    ) t
    where t.fname=#{field}

 

posted @ 2023-03-18 09:38  丶Ronnie  阅读(360)  评论(0)    收藏  举报