如何用shell脚本实现传递表id值?

lingxaiyidu 2012-08-30 02:24:16
如何用shell 脚本 实现 根据查询出的id 然后根据id 找出子表对应的记录然后删除子表中对应的记录??
比如说 父表parent ;子表child;
如下表结构:
show create table parent;
CREATE TABLE `parent` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`create_time` datetime DEFAULT '2012-01-01 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=85 DEFAULT CHARSET=utf8 ;
show create table child;

CREATE TABLE `child` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `par_ind` (`parent_id`),
CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=73 DEFAULT CHARSET=utf8

mysql> select id from parent where create_time<='2012-01-01' and create_time>='2012-01-01';
+----+
| id |
+----+
| 101|
+----+
1 row in set (0.00 sec)

将此值 id=101 传给 子表 child 将小于此id 的记录删除.
delete from child where id<101;
然后删除父表中的对应记录
delete from parent create_time<='2012-01-01';

请教大家 如何通过shell 实现 id的传递??




...全文
163 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lingxaiyidu 2012-08-31
  • 打赏
  • 举报
回复
呵呵 OK 了 错误信息也可以出来了
sh脚本也可以执行了 谢谢你
给分了哦
nicenight 2012-08-31
  • 打赏
  • 举报
回复
07 16 * * * sh ~/id2.sh> ~/test120831.log

……
crontab 里没有用户啊,你用 ~ 它到哪找去。。。
假设你的 ~ 就是 /home/user_name,就要这样:

07 16 * * * /home/user_name/id2.sh 2> /home/user_name/test120831.log
lingxaiyidu 2012-08-31
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
SQL code
sql="select id from parent where create_time<='2012-01-01' and create_time>='2012-01-01';"
id=$(mysql -h<IP> -u<用户> -p<密码> -……
[/Quote]



在crontab里设置才能将错误定向输出到一个文件中
07 16 * * * sh ~/id2.sh> ~/test120831.log
我这样写 什么错误信息也没有啊
请指点下啊
nicenight 2012-08-31
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

引用 2 楼 的回复:

当然,可能你也有你自己的需求,那就试试这样吧,没有测试环境:
SQL code
sql="select id from parent where create_time<='2012-01-01' and create_time>='2012-01-01';"
id=$(mysql -h<IP> -u<用户> -p<密码> --column-names=0 ……
[/Quote]

可能还是路径的问题,你可以在 crontab 里把错误输出定向到一个文件里,这样就好看到错误,进行分析了。
如果手工执行正常,则推测是这里,把 mysql 改为绝对路径试试:
id=$(mysql -uroot -pmysql --column-names=0 --execute="$sql")
lingxaiyidu 2012-08-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

当然,可能你也有你自己的需求,那就试试这样吧,没有测试环境:
SQL code
sql="select id from parent where create_time<='2012-01-01' and create_time>='2012-01-01';"
id=$(mysql -h<IP> -u<用户> -p<密码> --column-names=0 --execute="$sql")……
[/Quote]

这样不行啊 不执行啊
下面是我sh里面的内容 我用crontab执行
数据删不掉啊
sql="use DBA_DEMO;select id from parent where id<73 and id>71;"

id=$(mysql -uroot -pmysql --column-names=0 --execute="$sql")

sql=" USE DBA_DEMO ; delete from child where parent_id > $id \G"

/usr/local/mysql/bin/mysql mysql -uroot -pmysql --column-names=0 --execute="$sql"


请教高手指点 此段语句哪里不对
多谢
wwwwb 2012-08-30
  • 打赏
  • 举报
回复
呵呵,解决就好
lingxaiyidu 2012-08-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

引用 3 楼 的回复:

直接删除不行?
mysql -uroot -p123 -N -e "delete from child where id<(select id from parent where create_time<='2012-01-01' and create_time>='2012-01-01');"
mysql -uroot -p123 -N -e "delete……
[/Quote]


ok 了 忘记加 use dbname;了 谢谢
wwwwb 2012-08-30
  • 打赏
  • 举报
回复
直接在MYSQL中运行
delete from child where id<(select id from parent where create_time<='2012-01-01' and create_time>='2012-01-01'
结果如何
lingxaiyidu 2012-08-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

为什么不直接用 delete from child where id < (select id from parent where create_time<='2012-01-01' and create_time>='2012-01-01'); 呢,还要外调一下 shell 呀?
[/Quote]
主要是为了清理正式库中的数据 考虑到数据的安全性和正确性,所有想将sql 和shell 脚本分开写
现在先在测试库做测试.看能不能实现
而且我用

/usr/local/mysql/bin/mysql mysql -uroot -pmysql -e "SELECT * FROM
child where id<( select id from parent where create_time<='2012-01-01' and create_time>='2012-01-01')\G " -N >> ~/111.log
echo "---------------select over-------------">~/111.log
/usr/local/mysql/bin/mysql mysql -uroot -pmysql -e "delete FROM
child where id<( select id from parent where create_time<='2012-01-01' and create_time>='2012-01-01') \G " -N >> ~/111.log
echo "-------delete child over-----">~/111.log


这种方法通过crontab 实现 不能成功 不知道是哪里出的问题
所有两种方法都试验下 那种可行就用那种吧
lingxaiyidu 2012-08-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

直接删除不行?
mysql -uroot -p123 -N -e "delete from child where id<(select id from parent where create_time<='2012-01-01' and create_time>='2012-01-01');"
mysql -uroot -p123 -N -e "delete from parent cre……
[/Quote]

不起作用啊
我用crontab 定时执行
[@* ~]$ crontab -l
07 17 * * * sh ~/test.sh
[@* ~]$ more test.sh

echo "===============TEST ID_SAILING==================================" > ~/111.log

/usr/local/mysql/bin/mysql mysql -uroot -pmysql -e "SELECT * FROM
child where id<( select id from parent where create_time<='2012-01-01' and create_time>='2012-01-01'); " -N >> ~/111.log
echo "---------------select over-------------">>~/111.log
/usr/local/mysql/bin/mysql mysql -uroot -pmysql -e "delete FROM
child where id<( select id from parent where create_time<='2012-01-01' and create_time>='2012-01-01'); " -N >> ~/111.log
echo "-------delete child over-----">~/111.log


执行完到mysql里面查看 都还在啊
mysql> use DBA_DEMO;
Database changed
mysql> SELECT * FROM child where id<( select id from parent where create_time<='2012-01-01' and create_time>='2012-01-01');
+----+-----------+
| id | parent_id |
+----+-----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 2 |
| 4 | 3 |
| 5 | 4 |
| 7 | 23 |
| 8 | 55 |
| 9 | 56 |
+----+-----------+
8 rows in set (0.00 sec)



wwwwb 2012-08-30
  • 打赏
  • 举报
回复
直接删除不行?
mysql -uroot -p123 -N -e "delete from child where id<(select id from parent where create_time<='2012-01-01' and create_time>='2012-01-01');"
mysql -uroot -p123 -N -e "delete from parent create_time<='2012-01-01';"
nicenight 2012-08-30
  • 打赏
  • 举报
回复
当然,可能你也有你自己的需求,那就试试这样吧,没有测试环境:
sql="select id from parent where create_time<='2012-01-01' and create_time>='2012-01-01';"
id=$(mysql -h<IP> -u<用户> -p<密码> --column-names=0 --execute="$sql")

sql="delete from child where id<$id;"
mysql -h<IP> -u<用户> -p<密码> --column-names=0 --execute="$sql"
nicenight 2012-08-30
  • 打赏
  • 举报
回复
为什么不直接用 delete from child where id < (select id from parent where create_time<='2012-01-01' and create_time>='2012-01-01'); 呢,还要外调一下 shell 呀?

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧