從另一個查詢創建查詢

[英]Creating a Query from another query


Looking for a little assistance.. I am trying to create a query to use as a parameters for another query.

尋求幫助。我正在嘗試創建一個查詢,以作為另一個查詢的參數。

Here is what I am attempting..

這是我正在嘗試的。

I think I didnt make myself clear enough (big surprise) ignore any hard coded data in the queries, they first part is using variables sent in from the search page.

我想我沒有說得很清楚(非常令人驚訝)忽略查詢中的任何硬編碼數據,第一部分是使用從搜索頁面發送的變量。

The second query is what I really need to know how to accomplish. If I have to figure out how to generate the sql for selecting however many records i got in the first query,,,

第二個查詢是我真正需要知道如何完成的。如果我必須弄清楚如何生成sql來選擇我在第一個查詢中獲得的記錄,

SELECT distinct Invoice_Number   FROM invoices_details where check_number = '9999' and taxid = '9999999'

Which returns 3 invoice numbers..

返回3個發票號

What I need to do is to use those invoice numbers in another select...

我需要做的是在另一個選擇中使用這些發票號碼…

***Select claim_details from claim_d where invoice in ('xxxx','yyyy','zzzz')***

I am at a loss on if this is even possible..

如果可能的話,我真拿不准。

Any help would be appreciated.

如有任何幫助,我們將不勝感激。

Thanks

謝謝

Kevin

凱文

1 个解决方案

#1


4  

You can wrap that SELECT in your IN clause which is also known as subquery like

您可以在in子句中封裝SELECT,它也稱為subquery like

Select claim_details 
from claim_d 
where invoice in (
SELECT Invoice_Number   
 FROM invoices_details 
 where check_number = '9999' 
 and taxid = '9999999'
)

(OR) using a INNER JOIN like below (recommended approach)

(或)使用下面的內部連接(推薦方法)

select distinct claim_details 
from claim_d c
join invoices_details i ON c.invoice = i.Invoice_Number
where i.check_number = '9999' 
and i.taxid = '9999999';

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/08/09/7204aed0f7574e420a14ba72e07709a1.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com