簡體   English   中英

如何將查詢結果用作另一個查詢的輸入?

[英]How to use the result of a query as the input for another query?

我想將選擇查詢的結果用作另一個選擇查詢的輸入值。

這是一個例子:

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個選擇查詢,這些查詢取決於以前的選擇查詢的結果,因此我不確定聯接是否可行。 即名稱>姓氏>訂閱>讀者ID>設備ID>等

我正在嘗試查找記錄,然后將其刪除。 這是我要使用的實際查詢

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 */

通過in子句:

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

我希望這將有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM