简体   繁体   English

如何使用 SQL select 的结果来获取另一个表中的记录?

[英]How to use a result from a SQL select to get records in another table?

I'm having a hard time with creating a Sql that pull data from three tables.我很难创建一个从三个表中提取数据的 Sql。

Here are part of the structure of each table:以下是每个表的部分结构:

Customer table:
accnum   integer
name     string
address  string
Routeno  string
delno    integer

Invoice table:
accnum   integer
invnum   string
deldate  date
amount   float
routeno  string
vstatus  string

Card table:
accnum    integer
expdate   string

The result I need is all the customer that is on routeno 1, 2 and 3 then I need any invoices that have the delivery date (deldate) and the expdate of any that have a credit card on file.我需要的结果是所有在 routeno 1、2 和 3 上的客户,然后我需要任何具有交货日期 (deldate) 和任何有信用卡存档日期的发票。 Sorted by delno.按delno排序。

Select c.accnum, c.name, c.address, c.routeno, c.delno from customer c
where c.routeno in ('1','2','3')

From this result I need the following.根据这个结果,我需要以下内容。

Select i.invnum, i amount from invoice i
where i.deldate = '2020-05-27' and (vstatus <> 'V' or vstatus is null)
and i.accnum=c.accnum
and i.routeno in ('1','2','3')

Select e.expdate from Card
where e.accnum=c.accnum

I tried using Join, but then I only get the customer that have invoices I nned them all.我尝试使用加入,但后来我只得到了有发票的客户,我把它们都买了。

Select c.accnum, c.name, c.address, c.routeno, i.invnum, i.amount,  e.expdate from Customer c
left Join Card e on c.accnum=e.accnum 
left Join Invoice i on c.accnum=i.accnum
where i.deldate = '2020-05-27' and (vstatus <> 'V' or vstatus is null)
and i.accnum=c.accnum
and i.routeno in ('1','2','3')
order by c.routeno, c.delno

I need a result like this:我需要这样的结果:

accnum name      address    routeno  delno invnum amount expdate
000030 Smith     1 main st   1         1   A123    5.00   12/22
000030 Smith     1 main st   1         1   A125    8.00   12/22
000022 Knox      14 main st  1         2   A124    10.00   
000014 Vohs      20 main st  1         3   A119    4.00   11/21
000078 Bow       3 Camp Ave  1         4   A120    3.00
000015 Jordan    4 River rd  2         1   A118    11.00  10/23
000015 Jordan    4 River rd  2         1   A117    15.00  10/23

Thanks for any help.谢谢你的帮助。 KHJ KHJ

The link provided should point you the right direction.提供的链接应该为您指明正确的方向。 Another way approach this is to create an outer joined subquery with the desired filter criteria on invoice.另一种方法是使用发票上所需的过滤条件创建一个外部连接子查询。

  SELECT c.accnum,
         c.name,
         c.address,
         c.routeno,
         i.invnum,
         i.amount,
         e.expdate
    FROM Customer c
         LEFT JOIN Card e ON c.accnum = e.accnum
         LEFT JOIN
         (SELECT accnum, invnum, amount
            FROM Invoice
           WHERE     deldate = '2020-05-27'
                 AND (vstatus <> 'V' OR vstatus IS NULL)
                 AND routeno IN ('1', '2', '3')) i
             ON i.accnum = c.accnum
ORDER BY c.routeno, c.delno

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM