回答
不太 「乾淨」 的解決方案,但這個工作。
Linux或Unix終端:
prompt$ echo YourFileHeader > aFile.txt ; mysql -u YOUR_USER -p YOUR_DATABSE --execute="SELECT ..." >> aFile.txt
Windows命令窗口(你要輸入的命令逐個):
c:\> echo YourFileHeader > aFile.txt
c:\> mysql -u YOUR_USER -p YOUR_DATABSE --execute="SELECT ..." >> aFile.txt
MySQL會提示輸入您的密碼。
我確實相信select ... into outfile...
有點慢,我總是用這個解決方案得到更好的結果。下行:查詢輸出將有字段和行默認終止符(分別爲\t
和\n
)......我不知道是否有方法從shell中調整它。
希望這對你有用。
我終於使用了類似的東西,除了它在GNU/linux上。 謝謝。 – 2010-06-04 05:12:54
很長一段時間,因爲這個答案......但我提到的缺點...你可以在Linux/Unix中使用'sed'來調整它。你可以在這裏看到一個例子:http://lowfatlinux.com/linux-sed.html – Barranka 2012-05-29 19:08:24
這裏有一個不同的解決方案可能是好還是壞,根據您的情況:
http://jasonswett.net/how-to-get-headers-when-using-mysqls-select-into-outfile/
我們可以如下使用UNION功能實現這一目標。
SELECT * INTO OUTFILE "/tmp/COD-Shipment-Report.csv" FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM
(SELECT 'Shipment Id' , 'Invoice No' , 'POD Number' , 'Order Id' , 'Shipment Created' , 'Shipment Created-TS' , 'Weight' , 'Qty' , 'Total Amount' , 'Courier Agency' , 'Payment Method' , 'Billing State' FROM DUAL
UNION
SELECT shipment.increment_id 'Shipment Id', IFNULL(invoice.increment_id,'') 'Invoice No', shipment.track_no 'POD Number', shipment.order_increment_id 'Order Id', DATE_FORMAT(ADDTIME((shipment.created_at),'05:30:00'),'%d-%m-%Y') 'Shipment Created', ADDTIME(shipment.created_at,'05:30:00') 'Shipment Created-TS', shipment.shipping_weight 'Weight', shipment.total_qty 'Qty', sum(shpitm.qty*shpitm.price) 'Total Amount', shipment.track_title 'Courier Agency', payment.method 'Payment Method', IFNULL(shipping.region,'') 'Billing State' FROM sales_flat_shipment_grid shipment JOIN sales_flat_shipment ship ON shipment.entity_id=ship.entity_id JOIN sales_flat_invoice invoice ON invoice.order_id=ship.order_id JOIN sales_flat_shipment_item shpitm ON ship.entity_id= shpitm.parent_id JOIN sales_flat_order_payment payment ON shipment.order_id=payment.parent_id JOIN sales_flat_order_address shipping ON ship.shipping_address_id=shipping.entity_id WHERE payment.method='cashondelivery' AND DATE(ship.created_at) BETWEEN SUBTIME('2011-12-01 00:00:00', '05:30:00') and SUBTIME('2011-12-03 00:00:00', '05:30:00') GROUP BY shipment.entity_id) A
謝謝! Srinivas Mutyala
我想出了這個解決方案。這樣你就不必知道字段名稱。如果您只是想快速轉儲表格,這非常有用。
SET group_concat_max_len = 65533;
select @target:='sourcetablename'; -- the source table you're going to be dumping
select @ts:=replace(replace(now(), ':', ' '),' ',' '); -- a time stamp for the file name
select @csvoutfile:=concat('d:\\\\', @target, @ts, '.csv');
--
-- Pull the field names from the schema
--
select
@field_headers:=GROUP_CONCAT(CONCAT('\'', COLUMN_NAME, '\''))
from
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = @target
order BY ORDINAL_POSITION ;
--
-- build a select statement to include the field names and "union all" it with the table
-- and provide the outfile parameters
--
select
@betterexport:=concat('select ',
@field_headers,
' union all ',
' select * from ',
@target,
' ',
'into outfile \'',
@csvoutfile,
'\' FIELDS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \'"\' LINES TERMINATED BY \'\\n\';');
--
-- prepare and execute the query.
--
prepare qry1 from @betterexport;
execute qry1;
deallocate prepare qry1;
--
--
--
或者更好的是,這裏有一個程序的版本,所以你可以把它隨意(即 「呼dump_csv(」 表名 「);」。):
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `dump_csv`(in target varchar(255))
BEGIN
declare ts varchar(50);
declare csvoutfile varchar(255);
declare field_headers varchar(2048);
declare betterexport varchar(2048);
DECLARE curs CURSOR FOR select GROUP_CONCAT(CONCAT('\'', COLUMN_NAME, '\'')) from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = target order BY ORDINAL_POSITION ;
SET group_concat_max_len = 65533; -- max is normally 1024 and will cause problems on tables with a lot of fields
--
-- Pull the field names from the schema
--
OPEN curs;
fetch curs into field_headers;
CLOSE curs;
set ts=replace(replace(now(), ':', ' '),' ',' '); -- a time stamp for the file name
set csvoutfile=concat('d:\\\\', target, ts, '.csv'); -- build filename
--
-- build a select statement to include the field names and "union all" it with the target table
-- and provide the outfile parameters
--
set @betterexport=concat('select ',
field_headers,
' union all ',
' select * from ',
target,
' ',
'into outfile \'',
csvoutfile,
'\' FIELDS TERMINATED BY \',\' OPTIONALLY ENCLOSED BY \'"\' LINES TERMINATED BY \'\\n\';');
--
-- prepare and execute the query.
--
prepare qry1 from @betterexport;
execute qry1;
deallocate prepare qry1;
--
-- beer.
--
END
享受! :)
- 使用克里斯
- 1. SELECT INTO OUTFILE不能寫入文件
- 2. mysql&select into OUTFILE - 將二進制數據寫入文件
- 3. 如何使用SELECT INTO OUTFILE寫入除/ tmp以外的目錄?
- 4. 用Guzzle上傳文件後會將文件頭寫入文件
- 5. SELECT INTO OUTFILE不起作用?
- 6. 使用XLS文件,而不是保存CSV在MySQL使用SELECT INTO OUTFILE命令使用SELECT INTO OUTFILE查詢與XLS文件
- 7. 將參數寫入(聲明)頭文件
- 8. SELECT * INTO OUTFILE目錄
- 9. MySQL - SELECT * INTO OUTFILE LOCAL?
- 10. mysqldump vs select into outfile
- 11. 寫作GPG解密的文件到指定OUTFILE
- 12. mysql插入不能與select into outfile?
- 13. 使用MySQL INTO OUTFILE不寫入tmp
- 14. 讀取/寫入文件頭
- 15. 將錯誤寫入指定文件
- 16. 將指定的行寫入新文件
- 17. 如何將非select mysql語句的結果導入OUTFILE?
- 18. SELECT INTO OUTFILE權限被拒絕,但用戶可以寫入目錄
- 19. 將多個html文件的正則表達式結果寫入.txt outfile
- 20. MySQL select into outfile/tmp no output
- 21. SELECT INFO OUTFILE(ERRCODE:13),CentOS的
- 22. 將頭文件寫入python的excel文件
- 23. 使用sqlalchemy生成:SELECT * ... INTO OUTFILE「file」;
- 24. 將文件寫入
- 25. 指定OUTFILE路徑
- 26. 使用php將頭文件寫入csv中
- 27. 在ruby中寫入outfile的問題
- 28. 我如何能保存文件後,SELECT ... INTO OUTFILE「result.csv」
- 29. FileOutputStream將二進制文件寫入指定的文件夾
- 30. 將指令插入組件事件
轉儲到終端,它(在它已經與包頭)重定向到現有文件''>>(附加) – Amarghosh 2010-06-03 09:54:22
你如何轉儲到終端用select into OUTFILE ? – 2010-06-03 12:28:42