0

Im trying to have a sql output file from a query between three tables. Below is my code:

mysqldump --user=user --password=password --host=localhost dbname --where="SELECT v.ITEM_ID , v.CODE ,\
s.SENSOR , d.DESTINATIONCODE \
FROM  V_TABLE v, S_TABLE s ,D_TABLE d \
WHERE s.ITEM_ID = v.ITEM_ID \
AND s.CREATIONDATETIME <  '2014-2-16 00:00:00'\
AND v.DESTINATION_ID=d.ID" > /var/www/dumps/output_15_12_2017.sql

this give me an error:

mysqldump: Couldn't execute 'SELECT /*!40001 SQL_NO_CACHE */ * FROM `COMMANDSPOOL` WHERE SELECT v.ITEM_ID , v.CODE ,s.SENSOR , d.DESTINATIONCODE FROM  V_TABLE v, S_TABLE s ,D_TABLE d  WHERE s.ITEM_ID = v.ITEM_ID AND s.CREATIONDATETIME <  '2014-2-16 00:00:00' AND v.DESTINATION_ID=d.ID': You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT v.ITEM_ID , v.CODE ,s.SENSOR , d.DESTINATIONCODE FROM  V_TABLE' at line 1 (1064         )

maybe it is an error in using \

pikimota
  • 241
  • 1
  • 4
  • 15

1 Answers1

2

To use a query like that you could send it into the mysql client, (not the mysqldump utility) and then redirect that output to the file instead:

echo "SELECT v.ITEM_ID , v.CODE ,\
s.SENSOR , d.DESTINATIONCODE \
FROM  V_TABLE v, S_TABLE s ,D_TABLE d \
WHERE s.ITEM_ID = v.ITEM_ID \
AND s.CREATIONDATETIME <  '2014-2-16 00:00:00'\
AND v.DESTINATION_ID=d.ID\
" | mysql --user=user --password=password --host=localhost dbname > /var/www/dumps/output_15_12_2017.sql

If you're going this route then the --batch and --table options can be useful.

Jeremy Jones
  • 4,561
  • 3
  • 16
  • 26
  • this worked for me. First I log into mysql `mysql -u name -p dbname` and then run the query `select ..... into outfile '....sql';` – pikimota Dec 15 '17 at 10:59