35
 mysqldump -t -u root -p  mytestdb mytable --where=datetime LIKE '2014-09%'

This is what I am doing and it returns:

mysqldump: Couldn't find table: "LIKE"

I am trying to return all the rows where the column datetime is like 2014-09 meaning "all September rows".

0

4 Answers 4

70

You may need to use quotes:

mysqldump -t -u root -p  mytestdb mytable --where="datetime LIKE '2014-09%'"
6
  • 3
    The reason here is the shell breaks arguments on spaces by default, so it interprets that as three unrelated arguments. Quotes groups them all together.
    – tadman
    Oct 8, 2014 at 16:23
  • 1
    thanks. had to use double quotes then single quote as such mysqldump -t -u root -p mytestdb mytable --where="datetime LIKE '2014-09%'"
    – nodejsj
    Oct 8, 2014 at 16:23
  • Can you also add the source of your answer? Oct 22, 2019 at 9:47
  • @NikhilWagh what do you mean? I do not remember when I used a shell the first time, and quotes were one of the early gotchas I had to learn about, huh, I don't know, thirty years ago. Oct 23, 2019 at 0:55
  • For reference: dev.mysql.com/doc/refman/8.0/en/mysqldump.html Oct 23, 2019 at 7:24
5

Selecting dates using LIKE is not a good idea. I saw this method in one project. This causes huge DBMS load and slow system operation as no index by this table column used.

If you need to select date range use between:

where datetime between '2014-09-01' and '2014-09-30 23:59:59'
2
  • Index is used as long as you do not use the wildcard first so '2019-09%' will use the index as it does not include the wildcard first but '%2014-09%' will not
    – bhttoan
    Jun 9, 2019 at 19:08
  • 1
    The conversion to string might prevent using index. Some people also confirm that it is better not using like for date comparison: stackoverflow.com/questions/1629050/… Jun 11, 2019 at 5:56
1

Not the answer but just a notice, when using mysqldump it will automatically add DROP TABLE and CREATE TABLE to the export file, in case you don't want that add --skip-add-drop-table and --no-create-info with the command, like:

mysqldump -u root-p database_name table_name --skip-add-drop-table --no-create-info > export.sql
0

You missed "" for where clause . datetime column name datetime is not recommended. It is a data type as well.

mysqldump -u root -p mytestdb mytable --where="datetime LIKE '2014-09%'
" > mytable.sql;

After executing the command a prompt will ask for MySQL password. then check your current folder for generated mystable.sql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.