Requêtes pour faire du CSV avec MySQL

Voici une petite requête pour MySQL ≥ 5 qui peut être utile si vous cherchez à obtenir la liste des champs d’une table, pour l’exporter en tant qu’entête d’un fichier CSV par exemple.

select group_concat(column_name SEPARATOR ";")
from information_schema.columns
where table_schema = 'DB_NAME' and table_name = 'TABLE_NAME'
group by table_name;

La requête suivant permet d’exporter une table complète en CSV, sans passer par un langage de script (à condition d’avoir la permission FILE).

select *
into outfile '/tmp/foo.csv'
fields terminated by ';' optionally enclosed by '"'
lines terminated by '\n'
from DB_NAME.TABLE_NAME;

Plus d’infos dans la doc de MySQL.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!