2015-12-22 122 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 
}