想要在变量中获取数据库值

时间:2014-04-20 05:37:03

标签: mysql sql shell

我需要从shell编程中的变量中获取数据库中的值。我正在使用以下命令。但是收到错误。

applicant= `mysql -uroot -p123456 -e 'SELECT applicant FROM 
    leave where status="Applied" and applying_date= curdate() 
    order by applying_date' comviva|tail -1`;
ERROR 1064 (42000) at line 1: 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 'leave where status="Applied" and 
applying_date= curdate() order by applying_date' at line 1

1 个答案:

答案 0 :(得分:1)

LEAVEreserved word,你需要把它放在反引号中。此外,变量赋值中=后面不能有空格。

applicant=$(mysql -uroot -p123456 -e 'SELECT applicant FROM 
    `leave` where status="Applied" and applying_date= curdate() 
    order by applying_date' comviva|tail -1);