Question

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?

Was it helpful?

Solution

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';

OTHER TIPS

The output of DESCRIBE looks like a table, but DESCRIBE is not actually a query and thus cannot be treated as, for example, a subquery in a SELECT statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top