0
我這裏有問題...加入表自身
Table : history
|id |transaction|created_at |merchant_id|
|-----|-----------|-------------------|-----------|
|1 |400 |2015-10-12 11:08:37|33 |
|1 |500 |2015-10-15 09:38:22|33 |
|1 |600 |2015-10-21 14:47:12|22 |
|2 |100 |2015-09-26 10:48:27|31 |
|2 |500 |2015-09-30 11:18:07|27 |
|2 |300 |2015-10-02 17:33:57|31 |
我想時,即時通訊做查詢:
SELECT SUM(a.transaction)/COUNT(a.transaction) AS avg_trans
FROM history AS a GROUP BY a.id, a.merchant_id
Result:
|id |avg_trans|merchant_id|
|------|---------|-----------|
|1 |450 |33 |
|1 |600 |22 |
|2 |200 |31 |
|2 |500 |27 |
然後顯示avg_trans到錶的歷史,就像這樣:
|id |transaction|created_at |avg_trans|merchant_id|
|-----|-----------|-------------------|---------|-----------|
|1 |400 |2015-10-12 11:08:37|450 |33 |
|1 |500 |2015-10-15 09:38:22|450 |33 |
|1 |600 |2015-10-21 14:47:12|600 |22 |
|2 |100 |2015-09-26 10:48:27|200 |31 |
|2 |200 |2015-09-30 11:18:07|500 |27 |
|2 |300 |2015-10-02 17:33:57|200 |31 |
任何人都可以幫助我嗎?
哦,,謝謝先生......我不知道,我們可以使用子查詢。真棒。 –
先生..檢查我的更新問題 –