{"id":882,"date":"2022-02-08T18:10:07","date_gmt":"2022-02-08T12:10:07","guid":{"rendered":"https:\/\/mellowhost.com\/blog\/?p=882"},"modified":"2022-02-08T18:14:16","modified_gmt":"2022-02-08T12:14:16","slug":"how-to-create-a-csv-from-mysql-query","status":"publish","type":"post","link":"https:\/\/mellowhost.com\/blog\/how-to-create-a-csv-from-mysql-query.html","title":{"rendered":"How to create a CSV from MySQL Query"},"content":{"rendered":"\n<p><strong>Problem: <\/strong><\/p>\n\n\n\n<p>I have a SQL query, how can I create a CSV file from the SQL Query?<\/p>\n\n\n\n<p><strong>Solution<\/strong><\/p>\n\n\n\n<p>I had a customer looking for such an option. This can be easily achieved using &#8216;into outfile&#8217; attribute of MySQL. Let me provide an example query like the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">select * from wp_woocommerce_orders left join wp_posts on wp_posts.id = wp_woocommerce_orders.order_id into outfile '\/var\/lib\/mysql\/orders_list.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n';<\/pre>\n\n\n\n<p>The above query is joining the order table of woocommerce with the WordPress posts table, and putting the output file in \/var\/lib\/mysql\/orders_list.csv file, in which each field is being terminated by a comma (aka comma separated file) and after each row, it is terminating with a new line separator.  One thing you need to remember is if you are not root, then you should provide the path to store the file in a place where you can write the file, for example, your home directory, which could be like \/home\/yourusername\/something.csv<\/p>\n\n\n\n<p>Hope this helps others to know how to achieve this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: I have a SQL query, how can I create a CSV file from the SQL Query? Solution I had a customer looking for such an option. This can be easily achieved using &#8216;into outfile&#8217; attribute of MySQL. Let me provide an example query like the following: The above query is joining the order table &hellip; <a href=\"https:\/\/mellowhost.com\/blog\/how-to-create-a-csv-from-mysql-query.html\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to create a CSV from MySQL Query&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[670,671,87,668,581,669],"_links":{"self":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/882"}],"collection":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/comments?post=882"}],"version-history":[{"count":2,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/882\/revisions"}],"predecessor-version":[{"id":884,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/882\/revisions\/884"}],"wp:attachment":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/media?parent=882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/categories?post=882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/tags?post=882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}