0

I have a list of column names, that may or may not be in a specific MySQL database

Is it possible to query the DB to see if a column name exists? When i do not know which table it might be in?

If yes, how would I do so?

This question has an answer that assumes i know which table it is in. I do not MySQL: How to create Column if not exists?

this answer is close, but also assumes i know exactly where each column is (i am trying to automate insertion, and the column names that exist may be different from the column names i will insert in the future, but contain the same data) How to select specific rows if a column exists or all rows if a column doesn't

2 Answers2

0

thanks to mustaccio for pointing out the other question that explains the Information_schema.

with MySql you would query SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS; and get a list of all columns

i would add that it gives a list of ALL column names, even the ones generated by MySql

0

As you noted you can use the Information_Schema, and then know the tables the columns are part of as well:

SELECT *
    FROM   information_schema.columns
    LEFT JOIN information_schema.tables
          ON columns.table_name = tables.table_name 
         AND columns.table_catalog = tables.table_catalog`

SQL Fiddle Link:

http://sqlfiddle.com/#!9/9eecb/254846

Rick James
  • 78,038
  • 5
  • 47
  • 113