在shell脚本中执行mysql查询

时间:2014-07-03 05:00:01

标签: mysql sql linux bash shell

假设我有2个数据库nm和nm_history。我需要从两个数据库中获取记录。如果我在mysql中执行查询,它的工作正常。但是如果我在bin / bash中执行该查询,则会返回以下错误

**

mysql: option '-e' requires an argument
./get_rec.sh: line 4: subscriber_2: command not found
./get_rec.sh: line 4: a_party: command not found
./get_rec.sh: line 4: subscriber_2: command not found
./get_rec.sh: line 4: prov_channel: command not found
./get_rec.sh: line 4: subscriber_2: command not found
./get_rec.sh: line 4: created: command not found
./get_rec.sh: line 4: svc_mgmt_07: command not found
./get_rec.sh: line 4: created: command not found
./get_rec.sh: line 4: subscriber_2: command not found
./get_rec.sh: line 4: svc_mgmt_07: command not found
./get_rec.sh: line 4: subscriber_2: command not found
./get_rec.sh: line 4: a_party: command not found
./get_rec.sh: line 4: svc_mgmt_07: command not found
./get_rec.sh: line 4: msisdn: command not found
./get_rec.sh: line 4: svc_mgmt_07: command not found
./get_rec.sh: line 4: action_type: command not found
./get_rec.sh: line 4: SELECT nm.., nm.., nm.. AS Created, nm_history.. AS terminate FROM nm. INNER JOIN nm_history.  ON nm.. = nm_history..
WHERE nm_history..=2: command not found

**

这是脚本:

mysql -uUser -pPassword -hHostDB -e

查询:

SELECT S.`a_party`, S.`prov_channel`, S.`created` AS Created,M.`created` AS terminate FROM nm.`subscriber_1` S INNER JOIN nm_history.`svc_mgmt_06` M ON S.`a_party` = M.`msisdn` 
WHERE M.`action_type`=2;**

3 个答案:

答案 0 :(得分:7)

-e参数需要一个内联参数,如下所示:

mysql -uUSER -pPASS -hHOST -e "SELECT * FROM db.table;"

答案 1 :(得分:3)

你应该在shell脚本中编写查询字符串而不用反引号(`):

mysql -uUser -pPassword -hHostDB -e"SELECT S.a_party, S.prov_channel, S.created AS Created,M.created AS terminate 
FROM nm.subscriber_1 S 
INNER JOIN nm_history.svc_mgmt_06 M ON S.a_party = M.msisdn
WHERE M.action_type=2"

答案 2 :(得分:-1)

我遇到了完全相同的问题,而解决的问题是我将双引号更改为单引号

赞:

alexander@linux:~> mysql -u alexmack -pXXXXXXX -D eliminacion -e "SELECT `NOMBRE`,`FECHA_TERMINO`, DATE_ADD(`FECHA_TERMINO`, INTERVAL 1 DAY) 'FECHA_BLOQUEO',DATE_ADD(`FECHA_TERMINO`, INTERVAL 10 DAY) 'FECHA_BORRADO' FROM `excolaboradores` WHERE ESTADO = 'habilitado';"
NOMBRE: orden no encontrada
FECHA_TERMINO: orden no encontrada
FECHA_TERMINO: orden no encontrada
FECHA_TERMINO: orden no encontrada
excolaboradores: orden no encontrada
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ', DATE_ADD(, INTERVAL 1 DAY) 'FECHA_BLOQUEO',DATE_ADD(, INTERVAL 10 DAY) 'FECHA_' at line 1
alexander@linux:~> mysql -u alexmack -pXXXXXXX -D eliminacion -e 'SELECT `NOMBRE`,`FECHA_TERMINO`, DATE_ADD(`FECHA_TERMINO`, INTERVAL 1 DAY) 'FECHA_BLOQUEO',DATE_ADD(`FECHA_TERMINO`, INTERVAL 10 DAY) 'FECHA_BORRADO' FROM `excolaboradores` WHERE ESTADO = "habilitado";'
+---------+---------------+---------------+---------------+
| NOMBRE  | FECHA_TERMINO | FECHA_BLOQUEO | FECHA_BORRADO |
+---------+---------------+---------------+---------------+
| test001 | 2020-06-15    | 2020-06-16    | 2020-06-25    |
| test002 | 2020-06-18    | 2020-06-19    | 2020-06-28    |
| test003 | 2020-06-20    | 2020-06-21    | 2020-06-30    |
+---------+---------------+---------------+---------------+