Can DESCRIBE syntax be embedded in SELECT statement?

JimZ picture JimZ · Dec 25, 2013 · Viewed 8.7k times · Source

In MySQL, the syntax DESCRIBE can show a table's structure, but it cannot be embedded to normal statement; is there some tricky way to do it?

For example, this shows the table1 structure, returned as a table (but SQL statement does not think so)

DESCRIBE `table1`

But this doesn't work:

SELECT * FROM (DESCRIBE `table1`)

Is there a way to enable it?

I want to join the "table" created by DESCRIBE syntax, how can I do it?

Answer

Saharsh Shah picture Saharsh Shah · Dec 25, 2013

You can use COLUMNS table of INFORMATION_SCHEMA to get expected result as an alternate solution of DESCRIBE table option.

Try this:

SELECT COLUMN_NAME AS `Field`, COLUMN_TYPE AS `Type`, IS_NULLABLE AS `NULL`, 
       COLUMN_KEY AS `Key`, COLUMN_DEFAULT AS `Default`, EXTRA AS `Extra`
FROM information_schema.COLUMNS  
WHERE TABLE_SCHEMA = 'schemaName' AND TABLE_NAME = 'table1';