Error when trying export from Mysql to CSV - mysql

When I trying to export Mysql DB to CSV like SELECT * INTO OUTFILE '/tmp/test.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM reviewdb1;
I get error
ERROR 1046 (3D000): No database selected
However I sign reviewdb1 properly.
So I switch to reviewdb1
mysql> use reviewdb1
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SELECT * INTO OUTFILE '/tmp/test.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM reviewdb1;
ERROR 1146 (42S02): Table 'reviewdb1.reviewdb1' doesn't exist
and it seems like something wrong in my query (syntax)
Could you give me advice, what exactly ?
Thanks in advance.
Update:
it's dawn on me I should sign TABLE in the DATABASE which I want to export to .csv.
SELECT * FROM account_diff_preferences INTO OUTFILE '/var/lib/mysql-files/test.csv' FIELDS TERMINATED BY ',';
this command works fine, but may I export whole database like this or only one table from database per command accepted ?

now I realize it was pretty stupid question.
excuse me.

Related

mysql - export table to csv file

I've created a PHP application that books in jobs. When an employee clicks the
EXPORT CSV button, I want a CSV file to download with all the SQL table information. How can I go about doing this.
I've seen some posts saying to run this query:
SELECT * FROM SalesOrders; OUTPUT TO 'c:\\test\\jobs.csv'
SELECT * INTO OUTFILE ''c:\\test\\jobs.csv''
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM SalesOrders;

MySQL Exporting query output to CSV

[MySQL Workbench 6.2 on Windows7]
I can export the query results clicking in the MySQL workbench export icon. See image below:
However, I need to do this repeatedly in different loops, so I would like to include it in my script.
I have tried:
SELECT * from TABLENAME where ID = 123456 INTO OUTFILE 'C:/Users/username/Desktop/test.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n';
I also tried the filepath with \\ instead of /, and also with LINES TERMINATED BY '\r \n' as I have seen in other posts.
When I tried this, I get Permission Denied [Errcode 13] despite I have grated my user file permissions in the MySQL command client too with the following code:
USE mysql;
UPDATE user SET File_priv = 'Y' WHERE User = 'db_user';
FLUSH PRIVILEGES;
Any ideas why it is still not working? Any good alternative is also welcome!
This is due to folder access permission for mysql. So use below path to write csv file.
C:\\Users\\<user_name>\\AppData\\Local\\Temp
Let suppose your system has user name ABC then your query should be.
SELECT * from TABLENAME where ID = 123456 INTO OUTFILE 'C:\\Users\\ABC\\AppData\\Local\\Temp\\test.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n';

Update a file using mysql

I've a database and im trying to export the data from a table to a .csv file so that i can import the data as contact data. I can create the file and write to it using this syntax:
SELECT E_Name, Email INTO OUTFILE '/xampp/tmp/result.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM email WHERE 1
the problem is when i run this a 2nd time, i get an error that the file already exists. (im aware it exists as i've previously ran the query and it created it) What i would like to happen is to either, check the information is there from before and add the new information, or simpler again, overwrite the original file with the updated version.
could anyone throw some information on how to do this?
Thanks in advance,
Andrew
///////////edit\\\\\\\\\\\\\\
okay, decided to go for the timestamp method as ^^^^ cannot really be done. however im now running into an error for this aswell :$
CONCAT(SELECT E_Name, Email INTO OUTFILE '/xampp/tmp/Sample', DATE_FORMAT(now(), '%d%m%Y'), '.csv')
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM email WHERE 1
Can anyone help me with this concat problem? unexpected IDENT_QUOTED

mysqldump of recent records

Does mysqldump (or some other command) have an option to dump ONLY recently updated rows? I haven't been able to find anything in the docs about this. thx.
You can give mysqldump a where clause using the --where option. So if you have a column called "modified" in your table and you want all rows modified in the past 2 hours you could do something like this:
mysqldump my_schema my_table --where="modified > now() - interval 2 hour"
Use the "into outfile" syntax:
SELECT * FROM table INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
WHERE timestamp > ?
You can then import this later if necessary by using:
LOAD DATA INFILE '/tmp/result.txt' INTO table
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
Documentation on MySQL's site:
http://dev.mysql.com/doc/refman/5.5/en/select.html
http://dev.mysql.com/doc/refman/5.5/en/load-data.html
Define a view for the query you want. Then use mysqldump on the view.
From mysqldump manpage:
--where=´where_condition´, -w ´where_condition´
Dump only rows selected by the given WHERE condition. Quotes around the condition are mandatory if it contains spaces or
other characters that are special to your
command interpreter.
Examples:
--where="user=´jimf´"
-w"userid>1"
-w"userid<1"

How to Export SQL Query to TXT Using Command Line

I want to export select * from table results to a text file from the command line in linux. How should I do this?
Thanks,
Jean
look at link
you only need to add this to the query
select * from table INTO OUTFILE '/tmp/myfilename.txt'
you can improve this to csv file (using it in excel latter)
like :
INTO OUTFILE '/tmp/myfilename.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'