域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過
這篇文章主要介紹了基于postgresql查詢某個(gè)字段屬于哪個(gè)表的說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧。
一、場(chǎng)景還原
前端小林問我,它從前端的返回的某些字段,想知道那個(gè)表有這個(gè)字段,它想操作這個(gè)表……
二、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';
補(bǔ)充:postgresql數(shù)據(jù)庫查詢所有字段詳細(xì)信息,包括描述
把里邊的public改成相應(yīng)模式名(默認(rèn)為public,可不改)
table_name改成對(duì)應(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
申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!