How can we export some field(s) from MySQL table into a CSV file?

MySQLMySQLi Database

<p>It can be done by providing the column(s) names in the SELECT &hellip; INTO OUTFILE statement while exporting the data from MySQL table into a file. We are illustrating it with the help of the following example &minus;</p><h2 style=""><span style="font-size: 18px;">Example</span></h2><p>Suppose we are having following data from table &lsquo;Student_info&rsquo; &minus;</p><pre class="prettyprint notranslate">mysql&gt; Select * from Student_info; +------+---------+------------+------------+ | id &nbsp; | Name &nbsp; &nbsp;| Address &nbsp; &nbsp;| Subject &nbsp; &nbsp;| +------+---------+------------+------------+ | &nbsp;101 | YashPal | Amritsar &nbsp; | History &nbsp; &nbsp;| | 105 &nbsp;| Gaurav &nbsp;| Chandigarh | Literature | | 125 &nbsp;| Raman &nbsp; | Shimla &nbsp; &nbsp; | Computers &nbsp;| | 130 &nbsp;| Ram &nbsp; &nbsp; | Jhansi &nbsp; &nbsp; | Computers &nbsp;| | 132 &nbsp;| Shyam &nbsp; | Chandigarh | Economics &nbsp;| | 133 &nbsp;| Mohan &nbsp; | Delhi &nbsp; &nbsp; &nbsp;| Computers &nbsp;| +------+---------+------------+------------+ 6 rows in set (0.07 sec)</pre><p>Suppose we want only two columns &lsquo;id&rsquo; and &lsquo;Name&rsquo; from the above table to be exported into a file then the following query can export the values of only &lsquo;id&rsquo; and &lsquo;name&rsquo; from &lsquo;Student_info&rsquo; table into a file named &lsquo;student1.csv&rsquo; &minus;</p><pre class="prettyprint notranslate">mysql&gt; Select id, Name from Student_info INTO OUTFILE &#39;C:/mysql/bin/mysql-files/student1.csv&#39;; Query OK, 6 rows affected (0.07 sec)</pre><p>The above query will create a file named &lsquo;Student1.csv&rsquo; and export the values of columns &lsquo;id&rsquo; and &lsquo;name&rsquo; from &lsquo;Student_info&rsquo; table into it.</p>
Updated on 20-Jun-2020 09:24:22