Pregunta

I have written this simple query which would pull out all the data from table into a CSV file.

SELECT Group_concat(Concat(column_name)) 
FROM   information_schema.columns 
WHERE  table_name = 'subject_assignment' 
       AND table_schema = 'newschema2' 
UNION ALL 
SELECT (SELECT Group_concat('`', column_name, '`') 
        FROM   information_schema.columns 
        WHERE  table_name = 'subject_assignment' 
               AND table_schema = 'newschema2') 
FROM   subject_assignment 
INTO OUTFILE 'D:\\export\\asd.csv'

Now, the first bit works great but I have issues with the second part. Instead of pulling out data from columns specified in column list it just displays me all the column names over and over again.

Could you please suggest what I am doing wrong?

Thanks.

¿Fue útil?

Solución

In your second SELECT you do not select any column from subject_assignment. Instead, you're selecting single string value made from concatenated column names. And you're selecting it as many times as the row count of subject_assignment.

UPDATE:

If you want to dynamically create column names and then select data from them, see this: https://stackoverflow.com/a/17573774/925196

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top