2015-07-10 87 views
1

基本上,我想在一個表中使用while循環顯示下面的sql查詢的組合輸出。我有一個名爲的表,參與機構其中包含所有機構的唯一代碼和名稱。我也有交易表,其中每個機構可以是買方,賣方或兩者(是的,機構可以爲其兩個客戶進行交易)。在這裏好人的幫助下,我能夠使用查詢中指出的sql JOIN將參與機構表中的每個代碼與相應的名稱進行匹配。下面的第一個查詢將總結每個公司的所有購買價值,第二個查詢將爲他們的銷售做同樣的事情。但是,我想要顯示的表格將在while循環中爲每個公司提供[Sum(buy_value)+ Sum(sell_value)]。我如何使用mysql或php來實現這一點。 注意:交易表有兩列,分別是buy_firm_code和sell_firm_code。他們持有的記錄是相同的,取決於公司參與的交易的哪一方。 以下是我迄今爲止所做的。在while循環中合併兩個SQL查詢

<?php 
$con=mysqli_connect("localhost","db_user","password","database"); 

$total_value = 0; 
$buy_value = 0; 
$sell_value = 0; 
$firm_name = ""; 
$institution_code = ""; 
$display = ""; 

// Check connection 
if (mysqli_connect_errno()) 
{ 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$buy_sql="SELECT p.institution_code, p.institution, SUM(t.trade_value) 
value_bought FROM trades t JOIN participating_institutions p ON  p.institution_code = t.buy_firm_code GROUP BY t.buy_firm_code ORDER BY value_bought DESC"; 

if ($buy_result = mysqli_query($con, $buy_sql)) 
{ 
// Fetch one and one row for buy side 
while($row=mysqli_fetch_array($buy_result)) { 
    $buy_value .= $row['value_bought'].'<br>'; 
    $institution_code .= $row['institution_code'].'<br>'; 
    $firm_name .= $row['institution'].'<br>'; 

} 

} 

$sell_sql = "SELECT p.institution_code, p.institution, SUM(t.trade_value) value_sold FROM trades t JOIN participating_institutions p ON p.institution_code = t.sell_firm_code GROUP BY t.sell_firm_code ORDER BY value_sold DESC"; 

if ($sell_result = mysqli_query($con, $sell_sql)) 
{ 
// Fetch one and one row for sale side 
while($row=mysqli_fetch_array($sell_result)) { 
    $sell_value.= $row['value_sold'].'<br>'; 
    $institution_code .= $row['institution_code'].'<br>'; 
    $firm_name.= $row['institution'].'<br>'; 

} 

} 
$total_value = $buy_value + $sell_value; 
$display .= '<table><tr><td>'.$institution_code.'</td><td>'.$firm_name .'</td><td>'.$total_value.'</td></tr></table>'; 
echo $display; 

// Free result set 
mysqli_free_result($buy_result); 
mysqli_free_result($sell_result); 
mysqli_close($con); 
?> 

當我運行這兩個問題:

  1. 該公司重申,我猜是因爲我運行兩個查詢。我想要的是每個公司的一個輸出記錄。

  2. 我沒有得到每個企業總體市場,而我得到的總買入和第一條記錄的總銷售的

    Firmcode A: FirmName A: 20,000 
    Firmcode B: FirmName B: 40,000 
    Firmcode C: FirmName C: 50,000 
    

,而不是總和我得到這個

Firmcode A: FirmName A: 
Firmcode B: FirmName B: 20,000 
Firmcode C: FirmName C: 
+0

我可以建議你應用分而治之的方法。你在MySQL部分工作,並嘗試創建一個可以編寫PHP的SQL。此外,如果您創建一個像這樣的小提琴[鏈接](http://sqlfiddle.com/#!9/077e1/32)將更容易讓我們瞭解問題並幫助您解決問題。否則讀取這麼多的代碼將需要很長時間,並阻止其他人幫助 –

+0

1我看到的問題是,你串聯你的'$行['value_bought']' - >'$ buy_value。= $ row ['value_bought']'。
';'&&'$ row ['value_sold']' - >'$ sell_value。= $ row ['value_sold']。''
';'把它們變成字符串。所以當你試圖做''total_value = $ buy_value + $ sell_value;'你試圖添加字符串,而不是整數。 – Sean

+0

謝謝你們。我已經能夠解決這個問題。這是我做的。我用下面的代碼在我的數據庫中創建了兩個視圖。 _SELECT p.institution_code,p.institution,SUM(t.trade_value) value_bought從交易t JOIN participation_institutions p ON p.institution_code = t.buy_firm_code GROUP BY t.buy_firm_code ORDER BY value_bought DESC_ – Gyne

回答

0

謝謝你們。我已經能夠解決這個問題。我使用上面的兩個查詢在我的數據庫中創建了兩個視圖。然後,我在我的代碼中使用第三個查詢來使用SUM集合函數來加入這兩個視圖,以獲得我的總購買和總銷售價值。它現在完美。感謝您的貢獻。