mysql - 在 shell 脚本中执行 mysql 查询

标签 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;**

最佳答案

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

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

关于mysql - 在 shell 脚本中执行 mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24545276/

相关文章:

sql - 任何被赋予这一角色的人都应该能够向其他 ORACLE COMMAND LINE 分配权限

C# OleDB SQL 插入命令

linux - 列出包含字符串但 *NOT* 包含另一个字符串的文件的 Unix 命令

php - Mapbox:如何使用 PHP 在 Mapbox 输入搜索自动完成中获取地址值并插入到 Mysql 表中?

mysql - Azure 上具有不同关联组的数据库同步

java - java 中有关 SQL 语法的错误,而查询在 MySQL 中正常工作

c - 使用 switch 语句 fork 两个进程

linux - CMake:找不到没有绝对路径的库

MYSQL:根据唯一行值向表添加唯一数值?

带别名的 MySQL select 语句失败,未找到列