.sql文件查询使用变量

时间:2014-12-18 06:24:42

标签: mysql

我想像这样执行多个查询

Delete FROM TableA where CreatedBy="2"
Delete FROM TableB where CreatedBy="2"
Delete FROM TableC where CreatedBy="2"
Delete FROM TableD where CreatedBy="2"

我还想使用该id进行更多更新和插入查询。即使ID可能会发生变化 每次我必须改变价值来做到这一点 有没有办法声明变量并执行quires 例如

var userid=2;
Delete FROM TableA where CreatedBy="userid"
Delete FROM TableB where CreatedBy="userid"
Delete FROM TableC where CreatedBy="userid"
Delete FROM TableD where CreatedBy="userid"

如果我只改变一个地方,那个地方的名字会随处变化 我想创建一个sql文件并将其运行到我的MySQL服务器来更改用户标识 反正有吗?怎么样?

1 个答案:

答案 0 :(得分:1)

您可以这样设置会话变量:

SET @userid := '2';
DELETE FROM `TableA` WHERE `CreatedBy` = @userid;
DELETE FROM `TableB` WHERE `CreatedBy` = @userid;
DELETE FROM `TableC` WHERE `CreatedBy` = @userid;
DELETE FROM `TableD` WHERE `CreatedBy` = @userid;