Sql 使用select into outfile指令写入文件头?
我需要找到一种方法来插入文件头而不是列头,然后转储数据 你会怎么做Sql 使用select into outfile指令写入文件头?,sql,mysql,select,Sql,Mysql,Select,我需要找到一种方法来插入文件头而不是列头,然后转储数据 你会怎么做 在这方面,不是一个很干净的解决方案,但这是有效的 Linux或Unix终端: prompt$ echo YourFileHeader > aFile.txt ; mysql -u YOUR_USER -p YOUR_DATABSE --execute="SELECT ..." >> aFile.txt Windows命令窗口您必须逐个键入命令: c:\> echo YourFileHeader >
在这方面,不是一个很干净的解决方案,但这是有效的 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将提示输入您的密码
我确实相信。。。进入输出文件。。。是有点慢,我总是有更好的结果使用这个解决方案。缺点:查询输出将分别具有字段和行默认终止符\t和\n。。。我不知道是否有办法从shell中调整这个
希望这对您有用。根据您的情况,这里有一个不同的解决方案,可能更好,也可能更糟: 我们可以使用下面的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_csvtablename;:
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
享受吧
-Chris转储到终端,并使用>>附件将其重定向到已有的文件,其中已包含头文件。如何使用select into outfile将其转储到终端?我最后使用了类似的方法,只是它在GNU/linux上。谢谢。很久没有这个答案了。。。但是关于我提到的缺点。。。您可以在Linux/Unix中使用“sed”对其进行调整。您可以在此处看到一个示例: