gpt4 book ai didi

mysql - 使用变量查询.Sql文件

转载 作者:行者123 更新时间:2023-11-29 12:30:54 35 4
gpt4 key购买 nike

我想像这样执行多个查询

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 也可能会更改。
每次我都必须更改值才能做到这一点。
有什么方法可以声明变量并执行查询例如

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"

在这里,如果我只更改一个地方,则 id 会随处更改。
我想创建一个 sql 文件并将其运行到我的 MySQL 服务器并更改用户 ID。
有办法做到这一点吗?以及如何?

最佳答案

您可以这样设置 session 变量:

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;

关于mysql - 使用变量查询.Sql文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27540396/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com