php - Mysql如何根据条件分组?

标签 php mysql

我想在获取数据库的时候使用+=函数,但是当我使用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'];
}

因此,当用户查询 bid=1 时,$totalareaP 输出将是80。现在我的问题是,如果我想添加一个服务器任务(时间到了时自动运行查询)会将 $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 没有显示任何值时,我想通过 t1.areacode update 更新 $totalareaAAP 组进入 t3.areacode WHERE t1.areacode = t3.id

谢谢。

最佳答案

“group by”需要一个分组函数(在本例中为“sum”)

                                            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");

(注意 sum(t2.population) 作为人口)

在 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,每个“areacode”只会在结果中出现一次。在 PHP 中,$result 数组有一个条目,其中包含 MySQL 为该“区域代码”求和的总人口数。

显示结果

foreach ($result as $code => $population) {
   echo "Code $code => $population\n";
}

关于php - Mysql如何根据条件分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34408186/

相关文章:

php - 从外部 PHP 检查 Drupal 身份验证

Javascript 图表不从 mysql 填充数据

php - 使用 php ajax 和 mysql 使用 secret 问题重置密码

php - 我如何反转 mysqli 查询的结果顺序

php - 如何在使用 mysqli_multi_query 时进行 mysqli_fetch_row

mysql查询结果的php if语句(检查查询是否返回任何内容)

PHP 和 ZIP 创建 : Can standard zip "options" be applied

php - 按相似条件更新不同的表和列

php - 类别的嵌套函数

php - Laravel 如何设置帖子路由 ID?