需要在 sql table 中找到对应于特定订单 ID 集的装运 ID 计数,但查询有问题

Need to find count of shipment id which corresponds to certain set of order id in sql table but query is buggy

我有sales_ordertable

entity_id state    created_at
1         success  2020-05-01
2         fail     2020-05-01
3         success  2020-05-01
4         success  2020-05-01
5         process  2020-05-01
6         process  2020-05-01
7         process  2020-05-01
8         fail     2020-05-01
9         fail     2020-05-01

还有一个sales_shipmenttable

entity_id order_id   
1         1
2         1
3         2
4         2
5         3
6         4
7         4
8         4
9         5
10        6
11        6
12        6
13        6
14        7
15        7
16        8
17        9
18        9
19        9
20        9    

我想知道 sales_shipment table 中 entity_id 的计数是多少,它对应于 sales_order table 中的成功和处理状态订单

预期输出

Count
13

当我运行以下查询时,我没有得到想要的输出

select `entity_id` as tem, count(*) as cntsid  
from `sales_order` a 
inner join `sales_shipment` b on a.`entity_id`= b.`order_id`
where (`state` = 'success' or  `state` = 'process')  
group by tem

相反,我得到

Column 'entity_id' in field list is ambiguous

请帮忙查询

您需要在第一行的 entity_id 前面加上 table 名称或别名,在本例中我添加了 a.。否则引擎不知道使用哪一个(查询中有两个 entity_id 列)。

您的查询应如下所示:

select a.`entity_id` as tem, count(*) as cntsid  
from `sales_order` a 
inner join `sales_shipment` b on a.`entity_id`= b.`order_id`
where (b.`state` = 'success' or b.`state` = 'process')  
group by tem