mysqldump of recent records - mysql

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"

Related

PlanetScale : Import / Export to CSV in MySQL Shell requires Access to Servers Filesystem

If I want to do an export to csv or an import from csv, I will need access to the filesystem from within my MySQL Database Shell.
e.g.
SELECT id, filename
FROM attachments
INTO OUTFILE '/tmp/results.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
I am using PlanetScale right now, and i don't know how or where I can get access to the servers filesystem in order to import or export data from within the mysql shell.
See https://vitess.io/docs/14.0/reference/compatibility/mysql-compatibility/
The SELECT ... INTO form of SELECT in MySQL enables a query result to
be stored in variables or written to a file. Vitess supports SELECT
... INTO DUMPFILE and SELECT ... INTO OUTFILE constructs for unsharded
keyspaces but does not support storing results in variable. Moreover,
the position of INTO must be towards the end of the query and not in
the middle. An example of a correct query is as follows:
SELECT * FROM <tableName> INTO OUTFILE 'x.txt' FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\t' LINES TERMINATED BY '\n'
For sharded keyspaces this statement can still be used but only after
specifying the exact shard with a USE Statement.
(emphasis mine)
So you must know which server to use, because it's required that you specify the shard.

Error when trying export from Mysql to CSV

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.

How to export data to csv in mysql without the header?

MySQL has the functionality to export the data to a CSV file with following statement
SELECT
*
FROM
person
INTO OUTFILE 'person.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ','
ESCAPED BY '"'
LINES TERMINATED BY '\r\n';
But by default it will has the column name as the CSV header, so how to remove the header in this statement?
I reviewed the mysql reference but seems these is no such info.
2 options are provided in this post : How can I suppress column header output for a single SQL statement?)
1 : invoke mysql with -N flag will skip all column headers
2 : fake it
select column1 as '', column2 as '' from some_table;
After complete the csv file, seems for the huge records, the big csv file has no header, so if you want to add the header, following:
Several users asked about including headers, i.e., column names or variable names, in the "INTO OUTFILE" syntax.
One approach is to use the "--column-names" option in the mysql invocation:
mysql --column-names -e 'SELECT * FROM mysql.user' > test.dat
(This creates a tab-delimited file test.dat with column names in the first row followed by the query results.)
SELECT
*
FROM
person
INTO OUTFILE 'person.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ','
ESCAPED BY '"'
LINES TERMINATED BY '\r\n';
change FIELDS ENCLOSED BY '"' line
with OPTIONALLY ENCLOSED BY '"'

Exporting an SQL table to CSV with commas escaped?

I have a large MySQL table (8-9k records, I'm using phpMyAdmin) with some values that are descriptions, with commas.
I need to Export the SQL table into CSV form to use on a new platform and I can't for the life of me solve this issue:
When there are commas in certain fields, the data gets shoved to the next column and things get out of order.
I JUST NEED the EXACT table in MySQL to become a CSV. Any tips, ideas?
Thanks.
Use this query:
SELECT * INTO OUTFILE 'filename.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM [tablename]
Source: http://www.wausita.com/2011/02/mysql-export-csv/
Simply go to the database's Export tab. Be sure to enclose your fields. This way values are wrapped in " to avoid erroneous commas.
Otherwise, you can use mysqldump from the command line.
Or you can export data with a query:
SELECT *
FROM table
INTO OUTFILE '/tmp/table.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
My solution was exporting as an .xlsx from MySQL. The columns were rendered perfectly. Using several combinations of enclosing/escaping character was to no avail.
I then saved the .xlsx as a .csv and it was fine.
Thanks for your time guys.

MySQL "LOAD DATA INFILE" and Missing Double Quotes

I'm trying to load a CSV into MySQL using the LOAD DATA INFILE technique. It's working fine, but I have a problem where some columns use double quotes and some do not.
Example:
something,123,something,"Bauer, Jack",123,something
What happens is the commas inside the quotes break the import, so my data is all jacked up at the end. Not sure how to get the import to escape commas inside the double quotes.
mysql --user=<USER> --password=<PASS> -e "LOAD DATA INFILE '<FILENAME>' INTO TABLE <TABLENAME> FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' (col1, col2, col3, ...)" <DATABASE>
You need to execute the statement LOAD DATA INFILE with the additional option
FIELDS OPTIONALLY ENCLOSED BY '"'
Thus the whole statement becoming
LOAD DATA INFILE '<FILENAME>' INTO TABLE <TABLENAME>
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
(col1, col2, col3, ...)
For further readings, please consult the excellent MySQL Reference Manual e.g. for MySQL 5.1 GA.
Hopefully you have figured out what to do by now, but If you haven't hopefully this will help you out.
I have had trouble in Excel 2007 exporting to a customized CSV file. I found that you can change the fields terminator here: http://www.tek-tips.com/viewthread.cfm?qid=1635599&page=15
I prefer to use the pipe | character as it is uncommon, and a little more flexible when dealing with inch", foot" measurements.