`
yangyingda2008
  • 浏览: 15197 次
社区版块
存档分类
最新评论

linux下如何通过shell脚本查询数据库中的数据

阅读更多
先大家要熟悉在linux下如何连接数据库。连接数据库用法如下:

/usr/local/mysql/bin/mysql -hIP -uusername -ppassword -Pportnumber

-h面接数据库服务器的ip地址,-u参数接的是用户名,-p是数据库的密码,-P是数据库连接用的端口号。
2

那怎么查询数据呢?在shell中是不能用mysql的交互接口的,我们可以通过在建立连接的时候直接查询的方式实现。

/usr/local/mysql/bin/mysql -hIP -uusername -ppassword -Pportnumber

-e use lbc_inf;select count(name) from table_name where status=1 and update_time>$time_mill_sec_2_day_ago and update_time<$time_mill_sec_1_day_ago

注意上述命令是在一行中实现的!-e参数后面接的就是切换数据库和查询数据库的语句
3

下面展示一下一个完整的查询数据的脚本。脚本中通过时间来查询两天前和一天前数据表中新增的数据情况。

#!/bin/bash#查询数据库新增poi脚本#First release  2013/11/21 dataConn="/usr/local/mysql/bin/mysql -h10.10.18.234 -uusername -password -P3306" time_mill_sec_2_day_ago=`date --date='2 day ago' +%s`time_mill_sec_1_day_ago=`date --date='1 day ago' +%s`#echo -e "$time_mill_sec" result=`$dataConn -e "use database_name;select count(name) from table_name \ where status=1 and update_time>$time_mill_sec_2_day_ago and update_time<$time_mill_sec_1_day_ago"|awk 'END{print $0}'`; echo -e "`date --date='2 day ago' +%Y年%m月%d日`到`date --date='1 day ago' +%Y年%m月%d日`新增的生效的poi数量为:    $result"time_mill_sec=`date --date='1 day ago' +%s`#echo -e "$time_mill_sec"result=`$dataConn -e "use lbc_inf;select count(name) from table_name where status=1 and update_time>$time_mill_sec_1_day_ago"|awk 'END {print $0}'`;result=`$dataConn -e "use lbc_inf;select count(name) from table_name where status=1 "|awk 'END {print $0}'`;echo -e "查询完成!\n"



关于怎样通过sshpass来动态获取日志文件请看下面的文章。
http://jingyan.baidu.com/article/cd4c2979ca556c756e6e60aa.html

java中怎样使用linux的md5sun生成md5文件

camel骆驼男士凉鞋 真皮潮流魔术贴沙滩鞋男鞋 夏季新款正品凉鞋 只要56元!

http://redirect.simba.taobao.com/rd?w=unionnojs&f=http%3A%2F%2Fai.taobao.com%2Fauction%2Fedetail.htm%3Fe%3DYNbrUj%252FZdJwjmraEDZVrLkKA%252ByOYgzU6TbuPAry6zvGLltG5xFicOdXrTUTgh9sMDPIwxrc30rhF03SVjj78hGqYCHH8uv2oZb7Xhy%252F%252BGHmWC8e6JwspUeIZWR1bMnHu%26unid%3D96391090%26ptype%3D100010%26from%3Dbasic&k=5ccfdb950740ca16&c=un&b=alimm_0&p=mm_96391090_7268811_24064425

2014夏季新款韩版短裤女夏显瘦女裤修身热裤休闲裤子女士大码女装

原博客地址:
http://jingyan.baidu.com/article/af9f5a2dd8143b43140a4520.html
关于怎样通过sshpass来动态获取日志文件请看下面的文章。
http://jingyan.baidu.com/article/cd4c2979ca556c756e6e60aa.html
java中怎样使用linux的md5sun生成md5文件
http://jingyan.baidu.com/article/cd4c2979ca556c756e6e60aa.html
[/size][/size][/size][/size]
MySql数据库中null值与其他值的比较
http://jingyan.baidu.com/article/9113f81b2adc882b3214c7cb.html
Java编程经验分享之Map使用
http://jingyan.baidu.com/article/215817f7d9d6b31eda1423d8.html
MySql使用经验——通过in查询怎样排序问题
http://jingyan.baidu.com/article/67662997325ccf54d51b84d5.html
Mybatis使用经验之xml注意事项
http://jingyan.baidu.com/article/fec7a1e51d7b451190b4e729.html
Mybatis使用经验分享之批量操作
http://jingyan.baidu.com/article/11c17a2c7f376af446e39d21.html
MyBatis使用经验分享之查询
http://jingyan.baidu.com/article/af9f5a2dd8143b43140a4520.html
linux怎样搭建tomcat服务器
http://jingyan.baidu.com/article/3052f5a1d93c1497f31f860d.html
怎样用linux脚本查询数据中的数据
http://jingyan.baidu.com/article/2c8c281dfb0add0008252a04.html
linux编程实践经验sshpass、md5sum、ssh
http://jingyan.baidu.com/article/cd4c2979ca556c756e6e60aa.html


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics