{"id":134,"date":"2011-05-08T05:50:27","date_gmt":"2011-05-08T12:50:27","guid":{"rendered":"http:\/\/www.zulutown.com\/blog\/?p=134"},"modified":"2011-05-08T05:53:32","modified_gmt":"2011-05-08T12:53:32","slug":"importing-an-excel-spreadsheet-in-mysql-to-quickly-manipulate-its-data","status":"publish","type":"post","link":"http:\/\/www.zulutown.com\/blog\/2011\/05\/08\/importing-an-excel-spreadsheet-in-mysql-to-quickly-manipulate-its-data\/","title":{"rendered":"Importing an Excel spreadsheet in MySQL to quickly manipulate its data"},"content":{"rendered":"<p>I&#8217;ve been asked to compute some data and statistics from an Excel spreadsheet containing an huge phonebook<\/p>\n<p>The operations I need wasn&#8217;t very complicated, like to find and remove duplicate rows and so on, but I didn&#8217;t find any quick way to achieve those simple tasks using just Excel (or OpenOffice and LibreOffice).<\/p>\n<p>Since I&#8217;m good in SQL, I decided to move this data in a MySQL database, then I wondered what&#8217;s the simplest way to obtain this result.<\/p>\n<h2>Excel Data Structure<\/h2>\n<p>My excel data is on three simple columns:<\/p>\n<ul>\n<li>First Name<\/li>\n<li>Last Name<\/li>\n<li>Phone Number<\/li>\n<\/ul>\n<p>I need to export my 20,000 rows in a .csv file.<\/p>\n<h2>Export XLS Data in a CSV<\/h2>\n<h3>Using Microsoft Excel<\/h3>\n<p>From Excel, go in &#8220;Save As&#8221; then pick the option &#8220;Other Formats&#8221;, and from the combo box, choose Comma Delimited \u00a0CSV.<\/p>\n<p>Microsoft Excel \u00a0by default creates values separated by a Comma, and the single column is not enclosed by any special char.<\/p>\n<h3>Using OpenOffice or LibreOffice<\/h3>\n<p>In OpenOffice, choose Save As and then CSV, using the default options the .csv file will have values separated by semicolon and enclosed by double quotes.<\/p>\n<h2>Create the MySQL table to import the CSV<\/h2>\n<p>It&#8217;s time to create the basic data structure in MySQL that will be able to host the data we exported from Excel. So the simple task is to generate a table with the same number of columns (and type) that will be associated to the Excel columns.<\/p>\n<pre>create table phonebook (first_name varchar(100), last_name varchar(100), phone_number varchar(100))<\/pre>\n<p>And now, the last step, importing the CSV in MySQL<\/p>\n<h2>Import the CSV (generated from an XLS) into MySQL table<\/h2>\n<p>Mysql offers a useful command for the operation of importing the CSV in a table, the command is LOAD DATA LOCAL INFILE.<\/p>\n<p>And now the code in the case you exported the CSV from OpenOffice and the rows have the following structure:<\/p>\n<p>&#8220;Mario&#8221;,&#8221;Rossi&#8221;,&#8221;+390123456789&#8243;<\/p>\n<p>The code to load the data is:<\/p>\n<p><span style=\"font-family: Consolas, Monaco, 'Courier New', Courier, monospace; font-size: 12px; line-height: 18px; white-space: pre;\">load data local infile &#8216;phonebook.csv&#8217; into table phonebook fields terminated by &#8216;,&#8217; enclosed by &#8216;&#8221;&#8216; lines terminated by &#8216;\\n&#8217; (first_name, last_name, phone_number);<\/span><\/p>\n<p>If you exported using Microsoft Office the rows have the following structure:<\/p>\n<p>Mario;Rossi;+390123456789<\/p>\n<p>The code to load the data is:<\/p>\n<pre>load data local infile 'phonebook.csv' into table phonebook fields terminated by ';' enclosed by '' lines terminated by '\\n' (first_name, last_name, phone_number);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Microsoft Excel doesn&#8217;t offer a simple way to achieve some results that are very simple to obtain using SQL in MySQL, in this example it&#8217;s explained how to migrate some data from Excel to MySQL <a href=\"http:\/\/www.zulutown.com\/blog\/2011\/05\/08\/importing-an-excel-spreadsheet-in-mysql-to-quickly-manipulate-its-data\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[146,144,145,150,148,170,151,152,149,147],"_links":{"self":[{"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/posts\/134"}],"collection":[{"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/comments?post=134"}],"version-history":[{"count":9,"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"predecessor-version":[{"id":143,"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/posts\/134\/revisions\/143"}],"wp:attachment":[{"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zulutown.com\/blog\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}