dongxiezhi0590 2017-11-15 10:01
浏览 38
已采纳

如何在新col中插入由另一个col分组的col的总和

I'm trying to multiply 2 columns "qty*price_dl" of table and but the value in new column "total1" in the same table and this work with me without problems

after that trying to get sum of "total1" column grouped by "seq" column and but the values in "total2" column in the same table I'm stuck in process of summation it doesn't work with me this is the code:

<?php
require_once("dbconfig.php");
$q=mysql_query('select * from sale_menu')
or die(mysql_error());
echo "<table border='2'>
<tr>
    <th>Select</th>
    <th>qty</th>
    <th>price_dl</th>
    <th>total1</th>

</tr><tr>";
while($rw=mysql_fetch_array($q)){
    ?>
    <td><input type="checkbox" value="<?php echo $rw['seq']; ?>" name="seq[]" ></td>
    <td><b><?php echo $rw['qty']; ?></b></td>
    <td><b><?php echo $rw['price_dl']; ?></b></td>
    <td><b><?php echo $rw['total1']; ?></b></td>
    <tr>
    </tbody>
    <?php } ?>
    <?php
    error_reporting(0);
    $seq=$_POST['seq'];
    if (isset($_POST['submit']))
    {   
        $impid=implode(", ",$seq);
        $qdel=mysql_query("SELECT * FROM sale_menu WHERE seq IN(".$impid.")");
        while($row=mysql_fetch_array($qdel)){
            $qty=$row['qty'];
            $price_dl=$row['price_dl'];
            $qdel=mysql_query("UPDATE sale_menu set total1=qty*price_dl WHERE seq IN(".$impid.")");
            $qdel=mysql_query("UPDATE sale_menu set total2=sum(total1) WHERE seq IN(".$impid.")");
        }
    }
    ?>
    <button name="submit" type="submit" value="Checked" id="submit" >Move</button>

this is where I stuck:

$qdel=mysql_query("SELECT * FROM sale_menu WHERE seq IN(".$impid.")");
$qdel=mysql_query("select sum(total1) as total from sale_menu group by seq");
while($row=mysql_fetch_array($qdel)){
$total =  $row['total'];
$qdel=mysql_query("UPDATE sale_menu set total1=qty*price_dl WHERE seq IN(".$impid.")");
$qdel=mysql_query("UPDATE sale_menu set total2= $total WHERE seq IN(".$impid.")");
  • 写回答

1条回答 默认 最新

  • dongqie4402 2017-11-15 10:09
    关注

    You cant put total1 as column here because you need to make query inside query and there is two ways to do it

    The first it to do nested query like this

    $qdel=mysql_query("UPDATE sale_menu set total2= (select sum(total1) as total from table_name) WHERE seq IN(".$impid.")");
    

    The second is way much easier just make another query before this query and store the value of sum(total1) inside a variable then you can use the variable in this query for example

    $q=mysql_query('select sum(total1) as total from table_name')
    while($rw=mysql_fetch_array($q)){
    
       $total =  $rw['total'];
     }
    $qdel=mysql_query("UPDATE sale_menu set total2= $total WHERE seq IN(".$impid.")");
    

    I hope it works for you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助