這篇文章主要介紹了基于postgresql查詢某個字段屬于哪個表的說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧。
一、場景還原
前端小林問我,它從前端的返回的某些字段,想知道那個表有這個字段,它想操作這個表……
二、sql語句
SELECT *
FROM information_schema. COLUMNS
WHERE COLUMN_NAME = 'storename';
SELECT c.relname AS "表名",a.attname AS "字段名",c.oid
FROM pg_class c
LEFT JOIN pg_attribute a ON a.attrelid = c.oid
WHERE c.relname NOT LIKE 'pg_%'
-- AND c.relname = 'kx_store'
AND a.attname = 'storename';
補充:postgresql數(shù)據(jù)庫查詢所有字段詳細(xì)信息,包括描述
把里邊的public改成相應(yīng)模式名(默認(rèn)為public,可不改)
table_name改成對應(yīng)表名稱
SELECT AA.*,BB.DESC FROM
(
SELECT
A.ORDINAL_POSITION AS COL_NUM,
A.COLUMN_NAME AS COL_NAME,
A.DATA_TYPE AS COL_TYPE,
A.CHARACTER_MAXIMUM_LENGTH AS COL_LEN,
A.NUMERIC_PRECISION AS COL_PREC,
A.DATETIME_PRECISION AS COL_TIME_PREC,
A.IS_NULLABLE AS COL_NULLABLE,
A.COLUMN_DEFAULT AS COL_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS A
WHERE A.TABLE_SCHEMA = 'public' AND A.TABLE_NAME = 'table_name'
)
AS AA LEFT JOIN
(
SELECT
OBJSUBID,
DESCRIPTION AS DESC
FROM PG_DESCRIPTION
WHERE OBJOID =
(
SELECT RELID FROM PG_STAT_ALL_TABLES WHERE SCHEMANAME = 'public' AND RELNAME = 'table_name'
)
)
AS BB ON AA.COL_NUM = BB.OBJSUBID ORDER BY AA.COL_NUM;
文章來源:腳本之家
來源地址:https://www.jb51.net/article/204259.htm
申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點子。點擊此處,共同探討創(chuàng)業(yè)新機遇!