SELECT column_a,column_b,column_c+column_d INTO OUTFILE '/tmp/result.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '
'
FROM test_table;
<?php
$conn = mysqli_connect("localhost","root","test", "phppot_examples");
$query = "SELECT * FROM toy";
$result = mysqli_query($conn, $query);
$num_column = mysqli_num_fields($result);
$csv_header = '';
for($i=0;$i<$num_column;$i++) {
$csv_header .= '"' . mysqli_fetch_field_direct($result,$i)->name . '",';
}
$csv_header .= "
";
$csv_row ='';
while($row = mysqli_fetch_row($result)) {
for($i=0;$i<$num_column;$i++) {
$csv_row .= '"' . $row[$i] . '",';
}
$csv_row .= "
";
}
/* Download as CSV File */
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename=toy_csv.csv');
echo $csv_header . $csv_row;
exit;
?>
sqlcmd -S . -d DatabaseName -E -s, -W -Q "SELECT * FROM TableName" > C:Test.csv
SET @TS = DATE_FORMAT(NOW(),'_%Y_%m_%d_%H_%i_%s');
SET @FOLDER = '/var/lib/sql-files/';
SET @PREFIX = 'employees';
SET @EXT = '.csv';
SET @CMD = CONCAT("SELECT * FROM tableName INTO OUTFILE '",@FOLDER,@PREFIX,@TS,@EXT,
"' FIELDS ENCLOSED BY '"
' TERMINATED BY ','
ESCAPED BY '"'",
"LINES TERMINATED BY 'n';");
PREPARE statement FROM @CMD;
EXECUTE statement;