需要使用sql查询根据表1中的出现结果导出表2中的出现次数

xcitsw88  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(340)

表1包含订单id、国家/地区id的详细信息

table_ID   order_id   country_id
1          100        IN
2          200        USA
3          300        UK
4          400        IN
5          500        UK
6          600        UK
7          700        USA
8          800        USA
9          900        IN
10         1000       UK

表2包含装运标识、订单标识的详细信息

Shipment_ID   order_id   
1             100        
2             100        
3             100        
4             200        
5             200        
6             300        
7             300        
8             400        
9             500        
11            500
12            600
13            700
14            700
15            700
16            700
17            800
18            800
19            800
20            900
21            900
22            1000
23            1000
24            1000

我使用下面的查询来查找国家/地区的订单号列表

select `order_id`
from `Table_1` 
where `country_id` = 'IN'; 

order_id
100
400
900

我需要指导来编写查询以查找将Map到“in”中的订单id的装运id的计数
所以订单号100有3批货,400有1批货,900有2批货
期望最终输出

count_of_shipment_id
6
pwuypxnk

pwuypxnk1#

以下是您需要的查询:

SELECT country_id, count(*) as count_of_shipment_id
FROM Table_1 a
inner join Table_2 b on a.`order_id` = b.`order_id`
group by country_id

如果您只需要一个国家,您可以随时添加“where”或“having”来过滤结果。
在这里您可以看到您发布的示例:http://sqlfiddle.com/#!9/c90424/2号

相关问题