我有2张桌子。 t1
和t2
以下数据为t1
,kid
为planID,uid
为Userid,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 |
+---------------------------------+
我想在mysql中执行SUM
,如:
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
编辑我的编码?
答案 0 :(得分:2)
只需使用您提供的devtools::install_github('trinker/plotflow')
library(plotflow)
d1 = data.frame(x = rnorm(15),y = rnorm(15))
d2 = data.frame(x = rnorm(15),y = rnorm(15))
plotflow::ggdual_axis(
ggplot(data = d1, aes(x= x,y =y))+geom_line(),
ggplot(data = d2, aes(x= x,y =y))+geom_line()
)
- 这将导致40700:
sum
不确定您是否需要select sum((t1.buytime1 * t2.costb1) + (t1.buytime2 * t2.costb2))
from t1
join t2 on t1.kid = t2.id
- 如果是,那么您需要定义这些缺失值应该相等的内容。如果需要,使用outer join
应该有帮助。