mysql - 我想如何根据不同的属性汇总所有股票
问题描述
我想用DISTINCT显示属性并汇总每个属性的所有库存。喜欢
+----------+----------+
| attr | stock |
+----------+----------+
| Blue | 61 |
| Red | 119 |
+----------+----------+
这是我的桌子1
+----------+----------+
| attrid |attribute |
+----------+----------+
| 1 | Blue |
| 2 | Blue |
| 3 | Red |
| 4 | Red |
+----------+----------+
这是我的桌子2
+----------+----------+
| stock | attrid |
+----------+----------+
| 40 | 1 |
| 21 | 2 |
| 45 | 3 |
| 74 | 4 |
+----------+----------+
解决方案
连接两个表,对 stock 列求和,然后按属性 ID 分组,如下所示。
select sum(B.stock), A.attribute
from table1 A
inner join table2 B on (A.attrid = B.attrid)
group by A.attribute
推荐阅读
- angular - 茉莉花:无法读取未定义的属性“api”
- react-native - undefined 不是对象(tensorflow imagerecognition)
- audio - 直接从 python 程序播放音频(文本到语音)
- idris - Idris - 一般为接口创建可判定的属性/证明
- python - openpyxl单元格与边框合并?
- jq - jq:递归重命名键
- ios - Swift 故事板 - 使用 calloutaccessorycontroltapped 披露按钮在 MapView 中创建转场
- python - Plotly Legent Aa 标签如何删除它们
- python - 测试最佳神经元数量 - Keras/Tensorflow
- ios - 在 UserDefaults 中保存项目数组 - iOS 12