2010-06-03 41 views
0

我需要找到一種方法來插入文件頭(不是列頭),然後轉儲我的數據。用select將文件頭寫入outfile指令?

你會怎麼做?

問候,

+0

轉儲到終端,它(在它已經與包頭)重定向到現有文件''>>(附加) – Amarghosh 2010-06-03 09:54:22

+0

你如何轉儲到終端用select into OUTFILE ? – 2010-06-03 12:28:42

回答

1

不太 「乾淨」 的解決方案,但這個工作。

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中調整它。

希望這對你有用。

+0

我終於使用了類似的東西,除了它在GNU/linux上。 謝謝。 – 2010-06-04 05:12:54

+0

很長一段時間,因爲這個答案......但我提到的缺點...你可以在Linux/Unix中使用'sed'來調整它。你可以在這裏看到一個例子:http://lowfatlinux.com/linux-sed.html – Barranka 2012-05-29 19:08:24

1

我們可以如下使用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

0

我想出了這個解決方案。這樣你就不必知道字段名稱。如果您只是想快速轉儲表格,這非常有用。

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 

享受! :)

- 使用克里斯

相關問題