mysqldump with --where clause is not working

49,367

Solution 1

You may need to use quotes:

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

Solution 2

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'
Share:
49,367
nodejsj
Author by

nodejsj

Updated on June 10, 2020

Comments

  • nodejsj
    nodejsj almost 4 years
     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".

  • tadman
    tadman over 9 years
    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.
  • nodejsj
    nodejsj over 9 years
    thanks. had to use double quotes then single quote as such mysqldump -t -u root -p mytestdb mytable --where="datetime LIKE '2014-09%'"
  • bhttoan
    bhttoan almost 5 years
    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
  • Timofey Bugaevsky
    Timofey Bugaevsky almost 5 years
    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/…
  • Nikhil Wagh
    Nikhil Wagh over 4 years
    Can you also add the source of your answer?
  • crazy_in_love
    crazy_in_love over 4 years
    @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.
  • Nikhil Wagh
    Nikhil Wagh over 4 years
  • crazy_in_love
    crazy_in_love over 4 years
    Yeah, it says "[q]uotes around the condition are mandatory if it contains spaces or other characters that are special to your command interpreter". That's a feature of your shell.