SOLVE mysql linux bash named coloumn have a "-" and mysql query from file

I have a problem with mysql query on linux bash, my query has 2 coloumn with "-":

mysql -u root -pPass mydb -e "select `work-time` from work where `work-time`> '2013-0-3-07 00:00:00'"; 

but the result:

 bash: work-time: command not found 

I know the problem of this quote "", but how to solve this problem?

UPDATE MY REQUEST

SOLVE query in line :

 mysql -u root -pPass mydb -e "select \`work-time\` from work where \`work-time\`> '2013-0-3-07 00:00:00'" 

Another question

If I save my request in a file and request this one

  mysql -u root -pPass mydb < query.sql 

error received:

 ERROR at line 1: Unknown command '\`'. 

SOLVE query from a standard query file

 select `work-time` from work where `work-time`> '2013-0-3-07 00:00:00' 

save the request to a file, for example. query.sql and query as follows:

 mysql -u root -pPass mydb < query.sql 
+4
source share
2 answers

Mysql and using reverse ticks in a bash shell

A query like the one below will fail, because the backslash character has special meaning in the shell:

 mysql -u root -pPass mydb -e "select `work-time` from work" 

The back tick symbol must either be escaped or used inside the same quote when executing sql from the shell. Example of one line with quotes:

 mysql -u root -pPass mydb -e 'select `work-time` from work' 

To execute the same query using double quotes, we would need to avoid the reverse tick:

 mysql -u root -pPass mydb -e "select \`work-time\` from work" 

I suggest reading the difference between double quotes and single quotes in the bash shell.


Example Problem 1:

 "select `work-time` from work where `work-time`> '2013-0-3-07 00:00:00'" 

The above query will not work. However, you can do this with single quotes, but you will need to avoid any single quotes that are inside the query, for example:

 'select `work-time` from work where `work-time`> \'2013-0-3-07 00:00:00\'' 

Example Problem 2:

 "select \`work-time\` from work where \`work-time\`> \'2013-0-3-07 00:00:00\'" 

The above query is almost good, since you need to avoid the back tick, as it has special meaning. However, you do not need to avoid single quotes. With double quotes, the query might look like this:

 "select \`work-time\` from work where \`work-time\`> '2013-0-3-07 00:00:00'" 

Reverse ticks inside an SQL file

If you decide that you want to put this query in a file and send it to mysql, you will no longer need to avoid the back tick symbol, since the back tick symbol does not have much meaning inside the sql file. In the file, you simply put standard SQL:

 select `work-time` from work where `work-time` > '2013-0-3-07 00:00:00' 
+1
source

The text inside the backticks is interpreted as a command using bare or internal double quotes.

You need to use single quotes:

 mysql -u root -pPass mydb -e 'select `work-time` from work' 

In addition, you can avoid the reverse steps:

 mysql -u root -pPass mydb -e "select \`work-time\` from work" 
0
source

All Articles