微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我试图使用mysql中的count(user_Id)来计算列user_Id,如下所示:

 count=$(MysqL -uroot -proot csv_imports -e "select count(user_Id) from test_data where user_Id=\"12345\";")

我没有弄到它有什么问题.我想要它的数字结果.什么可以帮助我?

解决方法:

你的命令:

count=$(MysqL -uroot -proot csv_imports -e "select count(user_Id) from test_data where user_Id=\"12345\";")

可能会取这样的东西:

+---------------+
| count(userid) |
+---------------+
|             5 |
+---------------+

因为这是mySQL查询输出.

禁止标题和列名称,应包括选项-s(静)和-N(跳过列名称)

这样,MySQL命令只返回5(基于我的输出),它将使用以下内容存储在变量中:

count=$(MysqL -s -N -uroot -proot csv_imports -e "select count(user_Id) from test_data where user_Id=\"12345\";")

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

echo "$count"

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐