我想在获取数据库时使用+=
函数,但是当我使用group by
条件时,最终结果不是我想要的,所以请帮我查看我的编码。
我有3个表,表1 = t1
:
+-----------+-------------+-------------+-------------+
| ID | areacode | landstatus | pictureid |
+-----------+-------------+-------------+-------------+
| 1 | 1 | 0 | 0 |
| 2 | 1 | 0 | 0 |
| 3 | 1 | 4 | 1 |
| 4 | 1 | 4 | 2 |
| 5 | 1 | 4 | 1 |
| 6 | 1 | 2 | 1 |
| 7 | 1 | 4 | 4 |
| 8 | 1 | 1 | 0 |
| 9 | 2 | 0 | 0 |
| 10 | 2 | 4 | 1 |
+-----------+-------------+-------------+-------------+
表2 = t2
:
+-------+-------------+------------+
| ID | population | other |
+-------+-------------+------------+
| 1 | 10 | 0 |
| 2 | 20 | 0 |
| 3 | 30 | 0 |
| 4 | 40 | 0 |
+-------+-------------+------------+
通常我会像这样查询+-
:
让我们说bid=1
$bid = intval($_GET['bid']);
$queryAP = DB::query("
SELECT t1.*
, t2.population
FROM ".DB::table('t1')." t1
LEFT
JOIN ".DB::table('t2')." t2
ON t1.pictureid = t2.id
WHERE t1.areacode = '$bid'
AND t1.landstatus = 4
");
while($rowAP = DB::fetch($queryAP)) { //search all landstatus == 4
$totalareaP += $rowAP['population'];
}
当用户$totalareaP
80
时,query
输出将为bid=1
。现在我的问题是,如果我想添加服务器任务(自动运行查询时间为#39; up)将$totalareaP
更新为t3 where t2.arecode = t3.id
而不$_GET['bid']
。
表3称为:t3
。
+------+------------+-----------+
| ID | population | timesup |
+------+------------+-----------+
| 1 | 0 | timestamp |
| 2 | 0 | timestamp |
+------+------------+-----------+
我尝试编码:
$queryPPADD = DB::query("SELECT t1.*,t2.population FROM ".DB::table('t1')." t1 LEFT JOIN ".DB::table('t2')." t2 ON (t1.pictureid = t2.id) GROUP BY t1.areacode WHERE t1.landstatus = 4");
while($rowPPADD = DB::fetch($queryPPADD )) { //search all landstatus == 4
$totalareaAAP += $rowPPADD ['population'];
}
当我打印$totalareaAAP
没有显示任何值时,我想通过$totalareaAAP
更新为t1.areacode
更新t3.areacode WHERE t1.areacode = t3.id
谢谢。
答案 0 :(得分:1)
“group by”需要一个组功能(在这种情况下为“sum”)
.titled-pane > *.content {
-fx-border-color: transparent;
}
(请注意 vvv
$queryPPADD = DB::query("SELECT t1.areacode,sum(t2.population) as population FROM "
. DB::table('t1') . " t1 LEFT JOIN " . DB::table('t2')
. " t2 ON (t1.pictureid = t2.id) GROUP BY t1.areacode WHERE t1.landstatus = 4");
)
在PHP中你必须创建一个数组,
sum(t2.population) as population
PHP代码
array(areacode => population)
感谢 sum / group by ,每个“areacode”在结果中只出现一次。在PHP中,$result = array();
$queryPPADD = DB::query("SELECT t1.areacode,sum(t2.population) as population FROM "
. DB::table('t1') . " t1 LEFT JOIN " . DB::table('t2')
. " t2 ON (t1.pictureid = t2.id) GROUP BY t1.areacode WHERE t1.landstatus = 4");
while($rowPPADD = DB::fetch($queryPPADD)) {
$areacode = $rowPPADD ['areacode'];
$result[$areacode] = $rowPPADD ['population']; // just a =
}
数组有一个条目,其总人口由MySQL总结为“areacode”。
显示结果
$result
答案 1 :(得分:0)
$queryPPADD = DB::query("SELECT t1.*,t2.population FROM ".DB::table('t1')." t1 LEFT JOIN ".DB::table('t2')." t2 ON (t1.pictureid = t2.id) GROUP BY t1.areacode WHERE t1.landstatus = 4");
$totalareaAAP = 0;
while($rowPPADD = DB::fetch($queryPPADD )) { //search all landstatus == 4
echo $land = $rowPPADD ['landstatus'];// see what it is printing
echo $pic = $rowPPADD ['pictureid'];// see what it is printing
echo $pop = $rowPPADD ['population'];// see what it is printing
echo $totalareaAAP += $rowPPADD ['population'];// see what it is printing
}