11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 38 • IMPORTING AND EXPORTING DATAwww.it-ebooks.infoA Simple Data Export ExampleSuppose you want to export December, 2010 sales data to a tab-delimited text file, consisting of linesdelimited by newline characters:SELECT * INTO OUTFILE "/backup/corporate/sales/1210.txt"FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'FROM corporate.salesWHERE MONTH(order_time) = '12' AND YEAR(order_time) = '2010';Assuming that the executing user has SELECT privileges for the sales table found in the corporatedatabase, and the <strong>MySQL</strong> daemon process owner can write to the /backup/corporate/sales/ directory,the file 1210.txt will be created with the following data written to it:12309 45633 2010-12-19 01:13:42 22.04 5.67 27.7112310 942 2010-12-19 01:15:12 11.50 3.40 14.9012311 7879 2010-12-19 01:15:22 95.99 15.00 110.9912312 55521 2010-12-19 01:30:45 10.75 3.00 13.75Note that the spacing found between each column does not consist of white space, but rather is dueto the tab (\t) character. Also, at the conclusion of each line is the invisible newline (\n) character.Exporting <strong>MySQL</strong> Data to Microsoft ExcelOf course, by itself, outputting data to a text file really doesn’t accomplish anything except migrate it to adifferent format. So how do you do something with the data? For instance, suppose employees in themarketing department would like to draw a parallel between a recent holiday sales campaign and arecent rise in sales. To do so, they require the sales data for the month of December. To sift through thedata, they‘d like it provided in Excel format. Because Excel can convert delimited text files intospreadsheet format, you execute the following query:SELECT * INTO OUTFILE "/analysis/sales/1210.xls"FIELDS TERMINATED BY '\t', LINES TERMINATED BY '\n' FROM corporate.salesWHERE MONTH(order_time) = '12' YEAR(order_time) = '2010';This file is then retrieved via a predefined folder located on the corporate intra<strong>net</strong>, and opened inMicrosoft Excel. A window similar to Figure 38-1 will appear.If it isn’t already selected, choose the Delimited radio button, and click Next to proceed to the nextwindow, the second step of the Text Import Wizard. That window is shown in Figure 38-2.In the second step of the Text Import Wizard, choose the cell delimiter specified in the SELECT INTOOUTFILE statement. Clicking Next takes you to the final screen where you have the opportunity to convertany of the imported columns to one of Excel’s supported data formats; this task is not always necessary,but consider experimenting with the supported formats in case there is something more applicable foryour particular situation. Click Finish, and the data will open in normal Excel fashion.730

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!