dongzhen4180 2017-09-30 10:14
浏览 29

SQL查询从多个表和列和显示

i have a database with 2 different tables

One table (players) contain a column nammed "totaldeplata" that contains numbers (price) Other table (vanzari) contain a column like the first one nammed "totaldeplata" it hase the same value inside numbers (the price)

i manage to display the sum of all column for each table like this:

 <div align="right">   Total Comenzi luna curenta: <i><strong>
 <?php    
 $query = "SELECT * FROM vanzari WHERE MONTH(datainregistrarii) = 
 MONTH(CURRENT_DATE())";
 $query_run = mysql_query($query);

 $qty= 0;
 while ($num = mysql_fetch_assoc ($query_run)) {
     $qty += $num['totaldeplata'];
 }
 echo $qty;

   ?></strong>  </i> Lei
    </div> 

But this, display only the sum of one table, so i have to write the same code to display the sum of other column in the secound table (in this case the players table)

I need to sum all the numbers from columns "totaldeplata" from tables: vanzari and players, and display them as one number (to sum all the numbers and display them)

Actualy Sum numbers from columns "totaldeplata" from both tables "vanzari" and "players" and display them.

Any ideea how to do that? :)

  • 写回答

1条回答 默认 最新

  • douhuan3420 2017-09-30 10:34
    关注

    SELECT (SELECT SUM(field1) FROM table1) + (SELECT SUM(field2) FROM table2) as result

    According to your above code and table name.

    <?php
    $query = "SELECT (SELECT SUM(totaldeplata) FROM vanzari) + (SELECT SUM(totaldeplata) FROM players) as result";
    $query_run = mysql_query($query);
    $row = mysql_fetch_assoc($query_run); 
    $sum = $row['result'];
    echo "sum of two different column from two tables : "+ $sum;
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序