大家好,这个例子将输出什么mysql查询...
'table 1
| id |date |
| 1 |01/01/2015 |
| 2 |01/02/2015 |
| 3 |01/01/2015 |
| 4 |01/02/2015 |
'table 2
|id |table1_id| value1| value2| value3|value4|
| 1 | 1 | 5 | 2 | 4 | 4 |
| 2 | 2 | 40 | 3 | 2 | 3 |
| 3 | 3 | 5 | 2 | 4 | 5 |
| 4 | 4 | | 4 | 2 | 3 |
样品
$result = ($value1 * $value2) + ($value3 + $value4);
if($value1 == ""){
}else{
$result = ($value1 * $value2) + ($value3 + $value4);
}
与同一日期相加... 这是最终输出....
date finalvalue
01/01/2015 37
01/02/2015 125
先谢谢..
答案 0 :(得分:2)
我想你想做点什么:
SELECT table1.date, SUM(table2.value1) + SUM(table2.value2) + SUM(table2.value3) + SUM(table2.value4) AS finalvalue
FROM table1
INNER JOIN table2 ON table1.id = table2.table1_id
GROUP BY table1.date
不要在PHP中进行总结,你可以在SQL查询中更有效地完成它。
答案 1 :(得分:1)
我没试过这个(因为我没有设置mysql),但你可以尝试类似下面的内容:
SELECT t.date, t.$value1, t.$value2, t.$value3, t.$value4 FROM (SELECT t1.id, t1.date, $value1, $value2, $value3, $value4 from table t1, table t2 where t1.id == t2.table1_id group by t1.id) ) as t GROUP By t.date;
参考:https://dev.mysql.com/doc/refman/5.0/en/from-clause-subqueries.html来自SubQuery