2015-12-22 125 views
2

我想用+=函数取数据库,但是当我用group by这个条件时,最后的结果并不是我想要的,所以请帮我看看我的编码。Mysql如何通过条件组合?

我有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']; 
} 

因此当用户querybid=1$totalareaP输出将是80。现在我的问题是,如果我想添加一个服务器任务(自动运行查询,当时间到了)将更新$totalareaPt3 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更新来更新$totalareaAAP组到t3.areacode WHERE t1.areacode = t3.id

谢谢您。

+0

我没有看到你的T1连接到你的表2。我的意思是没有任何外键在T2 – learningbyexample

+0

't1.pictureid任何方式= t2.id',当't1.pictureid = t2.id'时,我从't2'查询'population' –

+0

你初始化$ totalareaAAP了吗? – learningbyexample

回答

1

“集团通过” 需要一组功能(在这种情况下, “总和”)

          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) as 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 = 
} 

感谢总和/组,每个“areacode”只会在结果中出现一次。在PHP中,$result数组有一个入口,其总数由MySQL为该“区域码”总和。

显示结果

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

是的,当't1.landstatus = 4'和't1.areacode'分开时,我需要'sum't2.population',所以'areacode = 1'上的'population'应该是'80','' areacode = 2'应该是'0' .. –

+0

答案已被编辑。 –

+0

我有尝试,解决我的问题,非常感谢你! –

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 
}