2013-11-03 29 views
1

希望你們好人!我在SELECT三個不同的表和SUM那裏總有一個大問題,以便我可以得到這三個表的總數。 表如下,我只是提一些領域:如何從三個不同的表中查詢總和的SELECT SUM php

1:支付

id idnumber school_fee trans_fee 
1 va03  10000  20000 

2:payment_one

id idnumber school_fee trans_fee 
1 va01  10000  30000 

3:payment_two

id idnumber school_fee trans_fee 
1 va02  40000  50000 

我已經從每張桌上得到'總計',我想要的是將我得到的總和加總到從這三張表中刪除。

這裏我的php代碼;

1:付款:

<?php 

    //include mysql connect 

    if (isset($_GET['query'])) 
{  
     $query=$_GET['query']; 

     // Instructions if $_POST['value'] exist  
     } 

    // gets value sent over search form 

    $min_length = 3; 
    // you can set minimum length of the query if you want 

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum 
      length then 

     $query = htmlspecialchars($query); 
     // changes characters used in html to their equivalents, for example: < to &gt; 

    $query = mysql_real_escape_string($query); 
    // makes sure nobody uses SQL injection 

     $raw_results = mysql_query("SELECT  
    *,SUM(school_fee+trans_fee) 
        As Total FROM payment 
      WHERE (`class` LIKE '%".$query."%')") or die(mysql_error()); 

    $raw_results2 = mysql_query("SELECT * FROM payment 
     WHERE (`class` LIKE '%".$query."%')") or die(mysql_error()); 





    // * means that it selects all fields, you can also write: `id`, `title`, `text` 
    // articles is the name of our table 

     // '%$query%' is what we're looking for, % means anything, for example if $query 
     is Hello 
    // it will match "hello", "Hello man", "gogohello", if you want exact match use 
     `title`='$query' 
     // or if you want to match just full word so "gogohello" is out use '% $query %' 
      ...OR ... '$query %' ... OR ... '% $query' 


    if(mysql_num_rows($raw_results) > 0){ 
    if(mysql_num_rows($raw_results2) > 0){ 
    // if one or more rows are returned do following 

     while($results = mysql_fetch_array($raw_results)){ 
     while($results2 = mysql_fetch_array($raw_results2)){ 

     // $results = mysql_fetch_array($raw_results) puts data from database into 
       array, while it's valid it does the loop 


      // posts results gotten from database(title and text) you can also show id 
        ($results['id']) 
     }{ 



       echo " &nbsp;Total amount of money payed by&nbsp;" .$results['class'] 
        ."&nbsp;"."class is&nbsp;" . $results ['Total'] . "&nbsp;/=Tshs"; 



       echo"<br>"; echo"<br>"; 
           } 


        } 
      } 
      } 
     } 

         ?> 

2:payment_one

<?php 

    //include mysql connect 

    if (isset($_GET['query'])) 
{  
     $query=$_GET['query']; 

     // Instructions if $_POST['value'] exist  
     } 

    // gets value sent over search form 

    $min_length = 3; 
    // you can set minimum length of the query if you want 

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum 
      length then 

     $query = htmlspecialchars($query); 
     // changes characters used in html to their equivalents, for example: < to &gt; 

    $query = mysql_real_escape_string($query); 
    // makes sure nobody uses SQL injection 

     $raw_results = mysql_query("SELECT  
    *,SUM(school_fee+trans_fee) 
        As Total FROM payment_one 
      WHERE (`class` LIKE '%".$query."%')") or die(mysql_error()); 

    $raw_results2 = mysql_query("SELECT * FROM payment_one 
     WHERE (`class` LIKE '%".$query."%')") or die(mysql_error()); 





    // * means that it selects all fields, you can also write: `id`, `title`, `text` 
    // articles is the name of our table 

     // '%$query%' is what we're looking for, % means anything, for example if $query 
     is Hello 
    // it will match "hello", "Hello man", "gogohello", if you want exact match use 
     `title`='$query' 
     // or if you want to match just full word so "gogohello" is out use '% $query %' 
      ...OR ... '$query %' ... OR ... '% $query' 


    if(mysql_num_rows($raw_results) > 0){ 
    if(mysql_num_rows($raw_results2) > 0){ 
    // if one or more rows are returned do following 

     while($results = mysql_fetch_array($raw_results)){ 
     while($results2 = mysql_fetch_array($raw_results2)){ 

     // $results = mysql_fetch_array($raw_results) puts data from database into 
       array, while it's valid it does the loop 


      // posts results gotten from database(title and text) you can also show id 
        ($results['id']) 
     }{ 



       echo " &nbsp;Total amount of money payed by&nbsp;" .$results['class'] 
        ."&nbsp;"."class is&nbsp;" . $results ['Total'] . "&nbsp;/=Tshs"; 



       echo"<br>"; echo"<br>"; 
           } 


        } 
      } 
      } 
     } 

         ?> 

3:payment_two

<?php 

    //include mysql connect 

    if (isset($_GET['query'])) 
{  
     $query=$_GET['query']; 

     // Instructions if $_POST['value'] exist  
     } 

    // gets value sent over search form 

    $min_length = 3; 
    // you can set minimum length of the query if you want 

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum 
      length then 

     $query = htmlspecialchars($query); 
     // changes characters used in html to their equivalents, for example: < to &gt; 

    $query = mysql_real_escape_string($query); 
    // makes sure nobody uses SQL injection 

     $raw_results = mysql_query("SELECT  
    *,SUM(school_fee+trans_fee) 
        As Total FROM payment_two 
      WHERE (`class` LIKE '%".$query."%')") or die(mysql_error()); 

    $raw_results2 = mysql_query("SELECT * FROM payment_two 
     WHERE (`class` LIKE '%".$query."%')") or die(mysql_error()); 





    // * means that it selects all fields, you can also write: `id`, `title`, `text` 
    // articles is the name of our table 

     // '%$query%' is what we're looking for, % means anything, for example if $query 
     is Hello 
    // it will match "hello", "Hello man", "gogohello", if you want exact match use 
     `title`='$query' 
     // or if you want to match just full word so "gogohello" is out use '% $query %' 
      ...OR ... '$query %' ... OR ... '% $query' 


    if(mysql_num_rows($raw_results) > 0){ 
    if(mysql_num_rows($raw_results2) > 0){ 
    // if one or more rows are returned do following 

     while($results = mysql_fetch_array($raw_results)){ 
     while($results2 = mysql_fetch_array($raw_results2)){ 

     // $results = mysql_fetch_array($raw_results) puts data from database into 
       array, while it's valid it does the loop 


      // posts results gotten from database(title and text) you can also show id 
        ($results['id']) 
     }{ 



       echo " &nbsp;Total amount of money payed by&nbsp;" .$results['class'] 
        ."&nbsp;"."class is&nbsp;" . $results ['Total'] . "&nbsp;/=Tshs"; 



       echo"<br>"; echo"<br>"; 
           } 


        } 
      } 
      } 
     } 

         ?> 

任何幫助I'will是令人欣慰的。

+0

[請停止使用mysql_ *函數](http://stackoverflow.com/q/12859942/1238019)在新代碼中,他們[正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。而不是,看看[準備好的陳述](http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html),並使用[Mysqli](http:///php.net/manual/en/book.mysqli.php)或[PDO](http://php.net/manual/en/book.pdo.php)。 – zessx

+0

您是否意識到您發佈的三個腳本完全相同,除了查詢中提到的表名('payment','payment_one'和'payment_two')?另外,在我看來,擁有這樣的三張表是一個非常糟糕的主意,除非您使用的是舊式(現有)數據庫 - 在這種情況下,我確實會使用視圖表來統一它們。 –

+0

是啊!是一樣的。表名除外 – Haika

回答

0

如果我理解正確的,你的所有三個表的[金] [payment_one] [payment_two]具有相同的列:ID,IDNumber中,school_fee trans_fee。

您將能夠使用一個單一的表來代替,並通過引入新的列區分開來:tableNum的,那麼這將是簡單的獲得你想要的東西。 請注意[id]和[tablenum]現在都是主鍵(複合主鍵)。

新表架構和數據將是(我不是很確定您的IDNumber列的目的):

[payment] 
id tablenum idnumber school_fee trans_fee 
1 0  va03  10000  20000 
1 1  va01  10000  30000 
1 2  va02  40000  50000 

SQL例如:

SELECT 
school_fee, 
trans_fee, 
(school_fee + trans_fee) as 'total' 
FROM payment WHERE id=1 
+0

那麼,這將帶來總計,注:本表爲單獨b'se每個類都有不同的收費, – Haika

+0

是的,這個統一表格應該成爲你的需要,並避免你有這些重複的代碼。 –

+0

idnumber是學生編號,每個班級有不同的費用,付款表中攜帶Baby類費用,payment_one爲標準費用,payment_two爲標準費用兩種,所以現在需要獲得所有班級的總和費用。 – Haika

相關問題