Mysql 使用 SUM 和 WHERE 查询不起作用

问题描述 投票:0回答:1
include 'conn.php';

$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'saleid';
$order = isset($_POST['order']) ? strval($_POST['order']) : 'desc';

$orderno = isset($_POST['orderno']) ? mysql_real_escape_string($_POST['orderno']) : ''; 

$offset = ($page-1)*$rows;
$result = array();

$where = "orderno like '$orderno%'"; 
$rs = mysql_query("select count(*) from tblsales where " . $where); 
$row = mysql_fetch_row($rs);
$result["total"] = $row[0];
$rs = mysql_query("select *, sum(price+shipping+paypalfee+storefee) as totalcost from tblsales group by orderno where " . $where . " order by $sort $order limit $offset,$rows");

$items = array();
while($row = mysql_fetch_object($rs)){
    array_push($items, $row);
}
$result["rows"] = $items;

echo json_encode($result);

您能否更改此查询,以便它在使用 WHERE 子句时返回表中的所有记录,以便我可以在需要时搜索特定记录。目前它没有返回任何内容:

  select *, sum(price+shipping+paypalfee+storefee) as totalcost
  from tblsales 
  group by orderno 
  where " . $where . " 
  order by $sort $order 
  limit $offset,$rows
sql mysql where-clause
1个回答
0
投票

试试这个

select *,sum(totalcost) 
FROM (select *, price+shipping+paypalfee+storefee as totalcost from tblsales) x
group by orderno 
where " . $where . " 
order by $sort $order 
limit $offset,$rows
© www.soinside.com 2019 - 2024. All rights reserved.