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

时间:2014-11-22 13:21:49

标签: mysql sql

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

1 个答案:

答案 0 :(得分: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