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

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度