0

If you don't know what are the columns(or how many columns are there) in a table, how to check whether a particular column exists or not in that table using MySQL?

For example,

A table "DemoTable" contains columns Col1, Col2, Col3 with some data. And you write a select query like,

Select col1, col2, col3, col4 from DemoTable. But col4 does not exists in the DemoTable, so it will through an error. How to check whether col4 exists in the DemoTable in the select query or before the select query? Without a procedure.

There are two tables with same name in two DB's, in those tables there are few columns with same name, while others columns have different names. For example, DemoTable has columns col1, col2, col3, col4 in one DB and in other DB, DemoTable has columns col1, col2, col3, col5, col6. But the condition is, a single select query should be used to fetch data from the table without an error, so we need to check whether that columns first exists or not. Query like Select col1, col2, col5 from DemoTable from any DB.

Ashu7390
  • 25
  • 1
  • 1
    possible duplicate of [MySQL, Check if a column exists in a table with SQL](http://stackoverflow.com/questions/3395798/mysql-check-if-a-column-exists-in-a-table-with-sql) // please do some research before asking. A simple search for [mysql check column exists](http://stackoverflow.com/search?q=mysql+check+column+exists) could have gotten you to the mentioned question (and lots of similar ones) on your own. – CBroe Jun 17 '15 at 18:06
  • I read those Q&A's, but did not find the solution without writing a procedure. Could it be done with a select query only? – Ashu7390 Jun 17 '15 at 18:23
  • There are two tables with same name in two DB's, in those tables there are few columns with same name, while others columns have different names. For example, DemoTable has columns col1, col2, col3, col4 in one DB and in other DB, DemoTable has columns col1, col2, col3, col5, col6. But the condition is, a single select query should be used to fetch data from the table without an error, so we need to check whether that columns first exists or not. Query like Select col1, col2, col5 from DemoTable from any DB. – Ashu7390 Jun 17 '15 at 20:48
  • Thank you for the suggestion Ryan – Ashu7390 Jun 17 '15 at 20:51

0 Answers0