gpt4 book ai didi

php - MYSQLI 查询总和列重复行上的值

转载 作者:行者123 更新时间:2023-11-29 07:32:51 26 4
gpt4 key购买 nike

添加所有列值 'price' of all duplicate invoice numbersORDERS-TABLE with _flag = y

我可以在 PHP 数组排序或 js 中实现这一点,但理想情况下,如果可能的话,希望在实际的 SQL 查询中实现这一点。

表_订单

_id    |    _invoice_num    |    _name    |    _price    |    _flag

0 123 bob 200 y
1 123 bob 300 y
2 555 mike 100 ...
3 123 bob 300 y
3 888 dave 200 y

PHP:

<?php
// im only after the query as its jsonen_code to ajax --

$sql = 'select * , select(sum(price)) from table_orders Where _flag ='y' ;

if ($results=mysqli_query($con,$sql)){

while ($row=mysqli_fetch_row($results)){

array_push($thearray,$row);

}
}

echo json_encode(array_values($thearray));

?>

输出:

 /* output expecting array length 2 rows

0 , 123 , bob , 800 , y
1 , 888 , dave, 200 , y

*/

添加:

 <?php

$the_type = 'the_flag';

//removes dupes
$sql = "SELECT * FROM orders WHERE $the_type = '' GROUP BY _invoice";

// need to sum the dupes now.

?>

最佳答案

这应该有效:

SELECT *,
SUM(_price) AS total
FROM _orders
WHERE _flag = 'y'
GROUP BY _invoice_num

关于php - MYSQLI 查询总和列重复行上的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50382596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com