Php Mysql如何按后条件分组?
我想在获取数据库时使用Php Mysql如何按后条件分组?,php,mysql,Php,Mysql,我想在获取数据库时使用+=函数,但是当我使用按条件分组时,最终结果不是我想要的,所以请帮助我查看我的编码 我有3个表,表1=t1: +-----------+-------------+-------------+-------------+ | ID | areacode | landstatus | pictureid | +-----------+-------------+-------------+-------------+ | 1 | 1
+=
函数,但是当我使用按
条件分组时,最终结果不是我想要的,所以请帮助我查看我的编码
我有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'];
}
因此,当用户查询bid=1
时,$totalareaP
输出将为80
。现在我的问题是,如果我想添加一个服务器任务(在时间'到'时自动运行查询),它会将$totalareaP
更新为t3,其中t2.arecode=t3.id
而不添加$\u 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'];
}
当我打印$totalAreaap
时没有显示任何值,我想通过t1.areacode
更新$totalareaAAP
分组到t3.areacode,其中t1.areacode=t3.id
谢谢。分组依据需要一个分组函数(本例中为“总和”)
(注意总和(t2.总体)作为总体
)
在PHP中,您必须创建一个数组
array(areacode => population)
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 =
}
由于sum/group by,每个“区号”在结果中只出现一次。在PHP中,$result
数组有一个条目,其中包含MySQL为该“区域代码”求和的总人口
显示结果
foreach ($result as $code => $population) {
echo "Code $code => $population\n";
}
我看不出t1与表2之间有任何连接。我的意思是在你的t2t1.pictureid=t2.id
中没有外键,我在t1.pictureid=t2.id
初始化$totalAreaap时从t2
中查询总体数
。嘿,你的sql语句看起来无效“groupbywhere”,加上您的+=null/由于左连接而未定义的值。可能会看到-尽管在这种情况下聚合似乎毫无意义。是的,我需要sum
t2.population
whent1.landstatus=4
并按t1.areacode
拆分。。因此areacode=1
上的population
应该是80
,areacode=2
应该是0
。。
foreach ($result as $code => $population) {
echo "Code $code => $population\n";
}
$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
}