从两个表计算商品剩余库存sql

Calculate the remaining stock of goods from two tables sql

我有 2 个 table
1.Table 交易在:

+-----+
|id_in|
+-----+
| 2   |
| 2   |
| 1   |
| 3   |
| 2   |
| 3   |
+-----+

2.Table 交易结束:

+------+
|id_out|
+------+
|   2  |
|   2  |
|   3  |
|   1  |
+------+

我想做一个查询,以便它根据类型在两个 table 之间产生差异 其中每个的数量由行数 (*) 确定 所以结果是

table查询结果

+--+---------------+----------------+----------------+
|id|count row id_in|count row id_out|(rowin - rowout)|
+--+---------------+----------------+----------------+
|1 |      1        |      1         |     0          | 
|2 |      3        |      1         |     2          |  
|3 |      2        |      2         |     1          |  
+--+---------------+----------------+----------------+

如何查询?

你可以试试下面的-

select idin, count_idin, count_idout,  count_idin-count_idout as result
from
(
  select idin,count(idin) count_idin from transactionin group by idin
)A inner join 
(
  select idout,count(idout) count_idout from transactionout group by idout
)B on A.idin=B.idout