原始表store:
| in | buy |
| A | 1000 |
| B | 2000 |
原始表market:
| id | sell |
| A | 100 |
| A | 200 |
| A | 300 |
| B | 500 |
| B | 400 |
SQL 语句如下:
select in, buy-sold from store, (select id, sum(sell) as sold from market group by id) as b where store.in = b.id
执行结果:
SQL联表查询示例
本文提供了一个SQL联表查询的实例,展示了如何从两个不同的表中(store和market)根据共同字段进行数据关联,并计算特定数值的过程。
原始表store:
| in | buy |
| A | 1000 |
| B | 2000 |
原始表market:
| id | sell |
| A | 100 |
| A | 200 |
| A | 300 |
| B | 500 |
| B | 400 |
SQL 语句如下:
select in, buy-sold from store, (select id, sum(sell) as sold from market group by id) as b where store.in = b.id
执行结果:
4325
373
2385

被折叠的 条评论
为什么被折叠?