gpt4 book ai didi

mysql - 如何将一个查询的结果作为另一个查询的输入?

转载 作者:行者123 更新时间:2023-11-30 22:47:44 27 4
gpt4 key购买 nike

我想使用一个选择查询的结果作为另一个选择查询的输入值。

这是一个例子:

SET @name = 'Tony';
select * from People where name=@name;

返回一个包含 2 列的表格名字姓氏

  • 托尼·丹萨
  • 托尼·贝内特

然后我想使用第一条记录的姓氏来查找订阅。

select * from Subscription where user='Danza';

是否可以使用占位符/公式而不是手动输入“Danza”?像这样的东西:

select * from Subscription where user=(result from first query);

我有 8 个选择查询,这些查询取决于之前选择查询的结果,所以我不确定连接是否可行。即姓名 > 姓氏 > 订阅 > readerID > deviceId > 等

我正在尝试查找记录,然后将其删除。这是我尝试使用的实际查询

SET @readerId=-7256784839031027017; // set manually
select * from Reader where readerId=@readerId;

SET @readersId=788216; // use the previous query's readerId column
select * from Reader_Device where READERS_ID=@readersId;

SET @deviceId=786527; // use the previous query's DEVICES_ID column
select * from Device where id=@deviceId;

SET @subscriptionValue='B1AA9B9FFBAE46918C079CAEC06EDC3B'; // use the previous query's deviceId column
select * from Subscription_attributes where KEY0='deviceId' and value=@subscriptionValue;

SET @subscriptionId=786618; // use the previous query's SUBSCRIPTION_ID column

delete from Installation where DEVICE_REF=@deviceId;
delete from Reader_Device where DEVICES_ID=@deviceId;
delete from Device where id=@deviceId;
delete from Subscription_attributes where KEY0='deviceId' and value=@subscriptionValue;
delete from Subscription_attributes where SUBSCRIPTION_ID=@subscriptionId;
delete from Subscription where id=@subscriptionId;
delete from Reader where readerId=@readerId;

最佳答案

通过加入:

select s.* from subscription s join people p on s.user = p.surname
where p.name = @name /* modify as per your requirement */

在子句中:

select * from subscription
where user in(select surname from people where name = @name)

希望对您有所帮助。

关于mysql - 如何将一个查询的结果作为另一个查询的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29117582/

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