Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can DESCRIBE syntax be embedded in SELECT statement?

Tags:

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?

like image 540
JimZ Avatar asked Dec 25 '13 05:12

JimZ


1 Answers

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';
like image 195
Saharsh Shah Avatar answered Sep 21 '22 03:09

Saharsh Shah