2013-10-15 22 views
0

我有我的MySQL數據庫2個表:PHP選擇形成一個MySQL表和秩序被另一個表

  • 客戶
  • customer_billing

客戶表有列列

  • 序列號
  • 公司

的* customer_billing *表有列

  • 序列
  • customer_seq

我對* customer_billing *表運行一個選擇查詢,然後一段時間內在客戶表中循環選擇查詢,如下所示:

$sql="SELECT *, SUM(quantity*unitprice) as customertotal from customer_billing where resellerid = '' and salesmanid = '' and producttype = '".$_GET["producttype"]."' group by customer_seq "; 
    $rs=mysql_query($sql,$conn) or die(mysql_error()); 
    while($result=mysql_fetch_array($rs)) 
    { 
     $sql2="SELECT * from customer where sequence = '".$result["customer_seq"]."' and company_status = '' "; 
     $rs2=mysql_query($sql2,$conn) or die(mysql_error()); 
     $result2=mysql_fetch_array($rs2); 
} 

我希望能夠從客戶表順序company ASC顯示的結果

我已經很明顯這只是訂單嘗試ORDER_BY customer_seq序列號

我也試着這樣做爲了通過company ASC客戶表查詢,但這沒有工作要麼

我怎麼能得到這個?

+0

mysql_query自PHP 5.5.0起棄用,並且將來會被刪除。相反,應該使用MySQLi或PDO_MySQL擴展。看到這個http://php.net/manual/en/function.mysql-connect.php –

+0

請提供示例數據和所需的輸出示例(最好的方法是提供[小提琴](http:// sqlfiddle。 com)) –

+0

它不是'order_by'。 –

回答

0

您應該在customer_seq上的客戶和customer_billing之間建立連接,並將公司添加到group by子句中。

這樣你應該可以按公司來訂購。

0

您可以使用Joins,無需使用second query試試這個,

$sql="SELECT c.*,ct.*, SUM(ct.quantity*ct.unitprice) as customertotal FROM 
    customer_billing ct , custom c WHERE ct.resellerid = '' AND ct.salesmanid = '' 
    AND c.company_status = '' AND c.sequence=ct.customer_seq AND 
    ct.producttype = '".$_GET["producttype"]."' 
    GROUP BY ct.customer_seq ORDER BY c.company ASC "; 
$rs=mysql_query($sql,$conn) or die(mysql_error()); 
while($result=mysql_fetch_array($rs)) 
{ 
    // your code 
} 
0

你需要這樣做使用join單個查詢。應該像這樣運行:

SELECT cb.customer_seq, c.sequence, c.company, SUM(quantity*unitprice) as customertotal 
FROM customer_billing AS cb JOIN customer AS c 
    ON cb.sequence = c.sequence 
WHERE cb.resellerid = '' 
    AND cb.salesmanid = '' 
    AND cb.producttype = '$_GET["producttype"]' 
    AND c.company_status = '' 
GROUP BY cb.customer_seq, c.company 
ORDER BY c.company 
+0

這只是顯示一個客戶記錄 – user2710234

+0

@ user2710234我修復了代碼,應該立即工作 – bazzilic

相關問題