Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use `SHOW COLUMNS` as a valid data source

I need to run the following query (this is a simplification of my process):

SELECT * 
FROM (SHOW COLUMNS FROM T1)

It errors out.

like image 766
Itay Moav -Malimovka Avatar asked Jun 14 '11 15:06

Itay Moav -Malimovka


2 Answers

This is what you want to do:

select * from (
    select * from INFORMATION_SCHEMA.COLUMNS 
    where table_name = 'T1'
) dt

You cannot use SHOW COLUMNS in a subquery, but by using the INFORMATION_SCHEMA.COLUMNS table, you have much more information available, not only the column name, for instance.

like image 79
Adriano Carneiro Avatar answered Nov 12 '22 19:11

Adriano Carneiro


See this post in SO Return order of MySQL SHOW COLUMNS

So probably you can use like as per the post

SELECT * FROM (SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
 WHERE table_name = 'tablename' 
 ORDER BY column_name) colinfo
like image 2
Rahul Avatar answered Nov 12 '22 17:11

Rahul