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 9977660080
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;
Related
I want to make a select to mySQL and return the resulting recordset as CSV but not saving it into a file.
I have this:
SELECT order_id,product_name,qty
FROM orders
WHERE foo = 'bar'
INTO OUTFILE '/var/lib/mysql-files/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
I have tried to change it into:
SELECT order_id,product_name,qty
FROM orders
WHERE foo = 'bar'
INTO #out
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
But it doesn't work.
The use case is to give users the option to download CSV files from data driven web site. To do so I need to output the CSV file/data to the browser.
How to do?
You can not directly and you need to import data first in a table.Mysql command for this https://dev.mysql.com/doc/refman/8.0/en/load-data.html
I need Select query which will at the end write the result into the file.
I would like to have as output columns with double quotes separated, and I would like to have comma between the fields, but I am getting an error that query is not correct.
select hostname,ip
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
FROM TABLE1
As output I would like to get:
"hostname1","ip1"
"hostname2","ip2"
...........
Is this possible?
Thanks
You can try below -
for reference, you can check here
SELECT hostname,ip INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
FROM test_table
I'm trying to figure out the syntax to export the results of a MySQL query to the desktop as a CSV file via SSH. Here's what I'm trying:
SELECT * INTO OUTFILE 'C:\Users\Jim Smith\Desktop\zyzyz.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\r\n'
from <table> limit 5;
The query itself works fine, but the file doesn't appear on the desktop. What should I change to get it to show up?
Try something like this
LINUS Path
SELECT *
FROM tableName
INTO OUTFILE '/tmp/fileName.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
And here is reference link http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/
[EDIT]
Take a look in Task Manager, if any instance of .csv file exists, then delete then try this
WINDOWS Path
SELECT * INTO OUTFILE 'd:\backups\php\export\data.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\'
LINES TERMINATED BY 'n'
FROM tableName;
I have loaded a table in mysql (xampp) with around 40,000,000 rows, with it I created another table with around 6,000,000 rows and I exported it to a csv file using:
(SELECT ...)
UNION
(SELECT ...
FROM ctr_train0
INTO OUTFILE 'C:/.../file.csv'
FIELDS ENCLOSED BY '"' TERMINATED BY ',' ESCAPED BY '"'
LINES TERMINATED BY '\n');
no errors, but this command creates a csv file with around 200,000 rows less than the original table, What happens? How can I export all the 6,000,000 rows?. Thanks in advance.
My best guess -- given the limited information -- is the use of union. This removes duplicates from the output, and the duplicates are both between tables and within tables. So, if your data has duplicates, this removes them.
Try running the query with union all instead:
(SELECT ...)
UNION ALL
(SELECT ...
FROM ctr_train0
INTO OUTFILE 'C:/.../file.csv'
FIELDS ENCLOSED BY '"' TERMINATED BY ',' ESCAPED BY '"'
LINES TERMINATED BY '\n');
I've got a csv file I made with a bunch of info, but I cant get it to import properly...
Ive got these values in info.csv: id firstname lastname address state gpa credits
following a video, I used this:
LOAD DATA LOCAL INFILE '/IT101/info.csv' INTO TABLE 'student' FIELDS TERMINATED BY ','
ENCLOSED BY '"' ESCAPED BY '\' LINES TERMINATED BY '\n';
I get back:
PAGER set to stdout
and the values aren't there. What am I doing wrong?
EDIT: By the way, I alaready have two rows in the table from using insert into, just doing it 30 more times seemed like a waste of time
You can try...
LOAD DATA INFILE '/IT101/info.csv'
INTO TABLE students
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;