如何在多个csv文件中运行多个查询和结果存储

时间:2015-02-02 10:05:32

标签: php mysql csv

如何运行mysql查询并在csv中输出结果。我知道解决方案应该是这样的:

SELECT col1,col2,col3
 FROM my_table
 INTO OUTFILE '/tmp/orders.csv'
 FIELDS TERMINATED BY ','
 ENCLOSED BY '"'
 LINES TERMINATED BY '\n';

但我的问题是,我有大约15个查询,我想运行所有15个查询和结果存储在多个csv文件中。当前对于第一个查询它工作正常并创建csv文件。但是没有为其他查询创建csv 。请帮助:)

查询1

SELECT CE.ENTITY_ID 'CUSTOMER ID'
 ,CE.EMAIL 'CUSTOMER EMAIL ADDRESS'
 ,CE.CREATED_AT
 ,CE.UPDATED_AT
 ,CE.IS_ACTIVE
 ,CE.STORE_ID
 ,(SELECT VALUE
      FROM CUSTOMER_ENTITY_VARCHAR CEV1
      WHERE CEV1.ATTRIBUTE_ID = 1 
      AND CEV1.ENTITY_ID = CE.ENTITY_ID) AS 'FIRST NAME'
 ,CEV.VALUE AS 'LAST NAME'
FROM CUSTOMER_ENTITY CE, CUSTOMER_ENTITY_VARCHAR CEV
WHERE CE.ENTITY_ID = CEV.ENTITY_ID
     AND CEV.ATTRIBUTE_ID = 2
INTO OUTFILE '$localDrive/DIM_CUSTOMER.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '`'
LINES TERMINATED BY '\n'";

QUERY2

SELECT LC.LOG_ID
 ,LC.VISITOR_ID
 ,LC.CUSTOMER_ID
 ,LC.LOGIN_AT
 ,LC.LOGOUT_AT
 ,LVI.HTTP_REFERER 'REFERRER SOURCE URL'
 ,LVI.HTTP_USER_AGENT
 ,LVI.HTTP_ACCEPT_LANGUAGE
 ,LVI.REMOTE_ADDR 
FROM LOG_CUSTOMER LC, LOG_VISITOR_INFO LVI
WHERE LVI.VISITOR_ID = LC.VISITOR_ID
INTO OUTFILE '$localDrive/AGG CUSTOMER LOGIN.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '`'
LINES TERMINATED BY '\n'";

******************************************解决**** *************

  

实际错误是我执行query.correct方式的方式如下:

$db->exec($query)

就是这样,它解决了我的问题:

1 个答案:

答案 0 :(得分:0)

实际错误是我执行query.correct方式的方式如下:

$db->exec($query);

就是这样,它解决了我的问题: