mysqldump用于特定ID和日期范围

时间:2018-03-27 06:47:12

标签: mysql backup

mysqldump -uroot -p --no-create-info --no-create-db hawa_new scada_data wind_turbine“SELECT s。* FROM scada_data as s,wind_turbine as w WHERE s.local_tm BETWEEN'2018-02 -01'AND'2018-02-28'AND s.turbine = w.id AND w.pooling_station = 29“| gzip> /var/lib/mysql-files/sd_feb.sql.gz

我得到了ERROR

无法找到表:“SELECT s。* FROM scada_data as s,wind_turbine as w WHERE s.local_tm BETWEEN'2018-02-01'''2018-02-28'AND s.turbine = w.id AND w.pooling_station = 29“

1 个答案:

答案 0 :(得分:0)

根据您是要转储INSERT语句还是将数据转换为CSV格式以在其他地方使用LOAD DATA INFILE,您可以采取两种方法。两个版本都假设您要使用gzip并可以访问命令行。

mysqldump方法

您可以使用mysqldump --where子句来过滤与第二个表中的值匹配的记录,但是您需要对现有的内容进行一些更改

更改你的--where子句以删除SELECT和FROM部分并匹配子选择中的wind_turbine记录

添加一个开关,以确保从数据库中获得一致的转储(--lock-all-tables或--single-transaction)。

从表列表中删除表wind_turbine,因为它与--where子句不匹配,您只想在scada_data中转储数据。

这样的事情应该有效:

mysqldump -uroot -p --single-transaction --no-create-info --no-create-db  --where "local_tm BETWEEN '2018-02-01' AND '2018-02-28' AND turbine IN (SELECT id FROM wind_turbine WHERE pooling_station = 29" hawa_new scada_data | gzip > /var/lib/mysql-files/sd_feb.sql.gz

选择INTO OUTFILE方法

(这与您原来的查询相同,可能就像我自己做的那样)

mysql -e "SELECT s.* INTO OUTFILE '/var/lib/mysql-files/sd_feb.sql' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'
FROM scada_data as s,wind_turbine as w WHERE s.local_tm BETWEEN '2018-02-01' AND '2018-02-28' AND s.turbine = w.id AND w.pooling_station = 29"; gzip /var/lib/mysql-files/sd_feb.sql