当我们把一个项目做完以后,客户要求我们把系统中所有的电话,证件号等进行加密处理时,我们难道要一个表一表去查看那些字段是电话和证件号码吗? 这种办法有点费劲,下面我们来探索如何找到想要的字段吧!
SELECT
t.table_name AS '表名',
t.COLUMN_NAME AS '字段名',
t.COLUMN_COMMENT AS '字段描述'
FROM
information_schema.COLUMNS t
WHERE
table_schema = 'xxx_industry_db'
AND column_name = 'region_id';
在数据库xxx_industry_db中查找具有字段名为 region_id的表,查询结果
SELECT
TABLE_NAME AS '表名',
COLUMN_NAME AS '列名',
COLUMN_COMMENT AS '注释'
FROM
information_schema.`COLUMNS`
WHERE
TABLE_SCHEMA = 'xxx_industry_db'
AND (COLUMN_COMMENT LIKE '%电话%'
OR COLUMN_COMMENT LIKE '%身份证%'
OR COLUMN_COMMENT LIKE '%号码%'
OR COLUMN_COMMENT LIKE '%手机%'
OR COLUMN_COMMENT LIKE '%联系方式%')
ORDER BY
TABLE_NAME,
ORDINAL_POSITION;
查找数据库xxx_industry_db列名包含 电话,身份证,号码,手机或联系方式的表及字段名,查询结果:
通过查询数据处带的表 information_schema.COLUMNS,我们还可以查更多我们想要查询的信息,比如我们还可以字段名称包含某些关键字的表及列:
SELECT
TABLE_NAME AS '表名',
COLUMN_NAME AS '列名',
COLUMN_COMMENT AS '注释'
FROM
information_schema.`COLUMNS`
WHERE
TABLE_SCHEMA = 'xxx_industry_db'
AND (COLUMN_NAME LIKE '%phone%' OR COLUMN_NAME LIKE '%mobile%' OR COLUMN_NAME LIKE '%card%' OR COLUMN_NAME LIKE '%no%' )
ORDER BY
TABLE_NAME,
ORDINAL_POSITION;