Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从三个不同的表中查询SELECT总和_Php_Mysql - Fatal编程技术网

Php 如何从三个不同的表中查询SELECT总和

Php 如何从三个不同的表中查询SELECT总和,php,mysql,Php,Mysql,希望你们是好人!我有一个很大的问题,选择三个不同的表,并在那里总和,以便我可以得到这三个表的总和。 下表中,我只提到了一些字段: 1:付款 id idnumber school_fee trans_fee 1 va03 10000 20000 2:一次付款 id idnumber school_fee trans_fee 1 va01 10000 30000 <?php //include mysql connect

希望你们是好人!我有一个很大的问题,选择三个不同的表,并在那里总和,以便我可以得到这三个表的总和。 下表中,我只提到了一些字段:

1:付款

id idnumber school_fee  trans_fee
1  va03     10000       20000
2:一次付款

id idnumber school_fee  trans_fee
1  va01     10000       30000
 <?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:第二次付款

id idnumber school_fee  trans_fee
1  va02     40000       50000
 <?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>"; 
                               }


                  }
            }
            }
       }

                      ?>
我已经从每个表中得到了“总计”,我想要的是把我得到的总计加起来,得到这三个表的总计

这里是我的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:一次付款

id idnumber school_fee  trans_fee
1  va01     10000       30000
 <?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:第二次付款

id idnumber school_fee  trans_fee
1  va02     40000       50000
 <?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>"; 
                               }


                  }
            }
            }
       }

                      ?>

任何帮助我都会感激的

如果我理解正确,您的三个表[payment][payment\u one][payment\u two]都有相同的列:id、idnumber、school\u fee trans\u 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

如果我理解正确,您的三个表[payment][payment\u one][payment\u two]都有相同的列:id、idnumber、school\u fee trans\u 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

在新代码中,它们是。您是否意识到,除了查询payment、payment\u one和payment\u two中提到的表名之外,您发布的三个脚本完全相同?另外,在我看来,拥有三个这样的表是一个非常糟糕的主意,除非您使用的是一个遗留的预先存在的数据库-在这种情况下,我会使用一个视图表来统一它们,真的。耶!是一样的。除了表名之外,您应该仔细阅读规范化的基础知识,并发现如果您将数据库修复为至少第二个标准形式,那么这个问题就很简单了。在新代码中,它们是。您是否意识到,除了查询payment、payment\u one和payment\u two中提到的表名之外,您发布的三个脚本完全相同?另外,在我看来,拥有三个这样的表是一个非常糟糕的主意,除非您使用的是一个遗留的预先存在的数据库-在这种情况下,我会使用一个视图表来统一它们,真的。耶!是一样的。除了表名之外,您应该阅读规范化的基础知识,发现如果您将数据库修复为至少第二个标准形式,那么这个问题很小。因此,这将带来总计,注意:这些表是单独的,因为每个类的费用不同,是的,这个统一的表应该满足您的需要,避免重复代码。idnumber是学生号,每个班级的费用不同,付款表上有婴儿班的费用,付款一是标准一费,付款二是标准二费,因此,现在需要获取所有课程费用的总和。“tablenum”列用于为您标识不同的课程,值0表示您的原始[payment]表,值1表示您的[payment\u one],值2表示您的[payment\u two]。因此,现在使用这个新模式,您只需要一个表,这使您的代码更加简单和干净。tablenum colum是自动递增的,还是?那么,这将带来总计,注意:这些表是单独的,因为每个类有不同的费用,是的,这个统一的表应该满足您的需要,避免重复代码。idnumber是学生号,每个班级的费用不同,付款表上有婴儿班的费用,付款一是标准一费,付款二是标准二费,因此,现在需要获取所有课程费用的总和。“tablenum”列用于为您标识不同的课程,值0表示您的原始[payment]表,值1表示您的[payment\u one],值2表示您的[payment\u two]。所以现在使用这个新模式,您只需要一个表,这使您的代码更加简单和干净?