Information Schema Duplicates - Specify which database to query in SELECT

1.1k Views Asked by At

I have two databases on my server with different names, but both of which have a table titled Clients. When I run an information schema query it returns columns from both databases, so I am getting duplicates when I just want one.

Example:

SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE table_name = 'Clients'

I'm getting duplicate columns so First_Name is showing up twice because it is returning the columns from the Clients table in both databases.

My question is:

is there a way of specifying which database to query in the SELECT statements so that it only returns the column from the one I want? For example:

SELECT COLUMN_NAME FROM information_schema.COLUMNS
WHERE db_name = 'DB1' AND table_name = 'Clients'
1

There are 1 best solutions below

0
On BEST ANSWER

You're looking for the TABLE_SCHEMA column.

SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE 
    TABLE_SCHEMA = 'DB1' AND 
    TABLE_NAME = 'Clients'

Just as an FYI, this is what I get when I do 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    |       |
+--------------------------+---------------------+------+-----+---------+-------+