在shell脚本中获取mysql的id计数

Asm*_*ita 3 bash shell-script mysql csv

我试图让列数USER_ID使用count(user_Id)来自MySQL的如下:

 count=$(mysql -uroot -proot csv_imports -e "select count(user_Id) from test_data where user_Id=\"12345\";")
Run Code Online (Sandbox Code Playgroud)

我不明白它有什么问题。我想要它的数字结果。什么能帮到我?

Lam*_*ert 5

你的命令:

count=$(mysql -uroot -proot csv_imports -e "select count(user_Id) from test_data where user_Id=\"12345\";")
Run Code Online (Sandbox Code Playgroud)

可能会获取类似的东西:

+---------------+
| count(userid) |
+---------------+
|             5 |
+---------------+
Run Code Online (Sandbox Code Playgroud)

因为这是mysql查询的默认输出。

要取消标题和列名,您应该包括选项-s(silent) 和-N(skip column names)

这样,该mysql命令仅返回5(基于我的输出)将使用以下方法存储在变量中:

count=$(mysql -s -N -uroot -proot csv_imports -e "select count(user_Id) from test_data where user_Id=\"12345\";")
Run Code Online (Sandbox Code Playgroud)

尝试count使用以下命令在终端中写入变量的值:

echo "$count"
Run Code Online (Sandbox Code Playgroud)

如果它只返回一个5(同样,基于我的输出),您可以将其用作测试表达式和计算中的数值。