用select into outfile指令写一个文件头?

时间:2010-06-03 09:51:45

标签: sql mysql select

我需要找到一种方法来插入文件头(而不是列标题)并在此之后转储我的数据。

你会怎么做?

此致

4 个答案:

答案 0 :(得分: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调整此值。

希望这适合你。

答案 1 :(得分: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

答案 2 :(得分:0)

根据您的具体情况,这可能是更好或更差的不同解决方案:

http://jasonswett.net/how-to-get-headers-when-using-mysqls-select-into-outfile/

答案 3 :(得分: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;
--
-- 
--

或者更好的是,这是一个程序版本,所以你可以随意调用它(即“call dump_csv(”tablename“);”):

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

享受! :)

- 克里斯