我的服务器上有两个不同名称的数据库,但两者都有一个标题为Clients
的表。当我运行信息模式查询时,它会从两个数据库中返回列,所以当我只想要一个时,我会得到重复项。
示例:
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE table_name = 'Clients'
我收到了重复的列,因此First_Name
出现了两次,因为它返回了两个数据库中Clients
表的列。
我的问题是:
有没有办法在SELECT语句中指定要查询的数据库,以便它只返回我想要的列?例如:
SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE db_name = 'DB1' AND table_name = 'Clients'
答案 0 :(得分:1)
您正在寻找TABLE_SCHEMA
列。
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_SCHEMA = 'DB1' AND
TABLE_NAME = 'Clients'
正如我所做的那样,这是我desc INFORMATION_SCHEMA.COLUMNS;
+--------------------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+---------------------+------+-----+---------+-------+
| TABLE_CATALOG | varchar(512) | NO | | | |
| TABLE_SCHEMA | varchar(64) | NO | | | |
| TABLE_NAME | varchar(64) | NO | | | |
| COLUMN_NAME | varchar(64) | NO | | | |
| ORDINAL_POSITION | bigint(21) unsigned | NO | | 0 | |
| COLUMN_DEFAULT | longtext | YES | | NULL | |
| IS_NULLABLE | varchar(3) | NO | | | |
| DATA_TYPE | varchar(64) | NO | | | |
| CHARACTER_MAXIMUM_LENGTH | bigint(21) unsigned | YES | | NULL | |
| CHARACTER_OCTET_LENGTH | bigint(21) unsigned | YES | | NULL | |
| NUMERIC_PRECISION | bigint(21) unsigned | YES | | NULL | |
| NUMERIC_SCALE | bigint(21) unsigned | YES | | NULL | |
| DATETIME_PRECISION | bigint(21) unsigned | YES | | NULL | |
| CHARACTER_SET_NAME | varchar(32) | YES | | NULL | |
| COLLATION_NAME | varchar(32) | YES | | NULL | |
| COLUMN_TYPE | longtext | NO | | NULL | |
| COLUMN_KEY | varchar(3) | NO | | | |
| EXTRA | varchar(30) | NO | | | |
| PRIVILEGES | varchar(80) | NO | | | |
| COLUMN_COMMENT | varchar(1024) | NO | | | |
| GENERATION_EXPRESSION | longtext | NO | | NULL | |
+--------------------------+---------------------+------+-----+---------+-------+