Export query data to CSV format from the MySQL console

0

I have the following query in MySQL , with which I get the posts associated with the users who created them

SELECT users.nameUser, posts.namePost
FROM users
JOIN posts ON users.id = posts.user_id;

However, I need to work directly from the console and at the time I execute this query I need to export the result of it to an external file in .csv format; I know there are visual environments like Heidi or Workbench to do that task quickly but I need to work from the console;

How do I get to do this task?

    
asked by element 21.09.2018 в 17:02
source

1 answer

0

At the console level of MySQL , we can run the console in the following way to export the data to an external file format

SELECT users.nameUser, posts.namePost INTO OUTFILE 'C:/respaldo.csv'
FROM users
JOIN posts ON users.id = posts.user_id;

What we are doing is:

  
  • The INTO OUTFILE statement dumps the table through the SQL statement to a particular file to be created in   that moment
  •   
  • The value that goes between the quotes, is the absolute path to where you are pointing to create the file that will contain the result   of your query SQL , you should check the name of the file and the   extension of the same so that the creation is done without problem
  •   
  • The INTO OUTFILE statement should go just before the FROM indicated by the first table referenced by the query
  •   
        
    answered by 21.09.2018 / 17:02
    source