1
我有2張桌子。 t1
和t2
Mysp Php Sum()乘法?
數據如下t1
,kid
是planID,uid
是用戶ID,buytime
是購買量。
+---------+-------------+-------------+---------------+
| kid | uid | buytime1 | buytime2 |
+---------+-------------+-------------+---------------+
| 1 | 1 | 10 | 5 |
| 1 | 2 | 1 | 0 |
| 1 | 3 | 3 | 5 |
| 2 | 4 | 1 | 10 |
| 3 | 5 | 20 | 6 |
+---------+-------------+-------------+---------------+
t2
數據:
+---------+-----------+-----------+
| ID | costb1 | costb2 |
+---------+-----------+-----------+
| 1 | 100 | 200 |
| 2 | 300 | 500 |
| 3 | 1000 | 2000 |
+---------------------------------+
我願做SUM
在MySQL一樣:
SUM buytime1 * costb1 + buytime2 * costb2 Where t1.kid = t2.id
$query = DB::query("SELECT SUM(something like buytime1 * costb1 + buytime2 * costb2) FROM ".DB::table('t1')." t1 LEFT JOIN ".DB::table('t2')." t2 ON (t1.kid = t2.id)");
所以最終的結果應該是40700
10 * 100 + 5 * 200 = 2000
1 * 100 + 0 * 200 = 100
3 * 100 + 5 * 200 = 1300
1 * 300 + 10 * 500 = 5300
20 * 1000 + 6 * 2000 = 32000
那麼,我應該編輯我的編碼query
?