开发者

How to output to csv file using mysql?

开发者 https://www.devze.com 2023-02-18 02:17 出处:网络
This is a sample table of my original table USER_DETAILS, i want to export the user_id and user_phone fields to a csv file for further implementation in my project.

This is a sample table of my original table USER_DETAILS, i want to export the user_id and user_phone fields to a csv file for further implementation in my project.

user_id    user_phone

1          9977660050
2          9977660051
3          9977660042
4          9977660开发者_如何学JAVA080

P.S.: Please answer me the query for this, instead of giving abrupt answers and suggestions. i guess the table is quite clear to understand.


If you don't really need this to be in PHP, just run:

SELECT user_id, user_phone INTO OUTFILE '/your/filepath'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  FROM user_details;

Obviously, replace '/your/filepath' with the path to the file you want to save to.


Not sure if you want to do this in PHP or just output it to a CSV file. Here's how to do the latter:

SELECT user_id, user_phone
FROM user_details
INTO OUTFILE '/tmp/user_details.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'


SELECT 'User','User Phone'

UNION

SELECT user_id,user_phone INTO OUTFILE 'users.csv'

FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'

FROM USER_DETAILS;

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号