大家! 我有3张桌子: order_product,产品和产品。
我有这个功能:
public function getOrders($data = array()) {
$sql = "SELECT MIN(o.date_added) AS date_start, MAX(o.date_added) AS date_end, COUNT(*) AS `orders`, SUM((SELECT SUM(op.quantity) FROM `" . DB_PREFIX . "order_product` op WHERE op.order_id = o.order_id GROUP BY op.order_id)) AS products, SUM((SELECT SUM(ot.value) FROM `" . DB_PREFIX . "order_total` ot WHERE ot.order_id = o.order_id AND ot.code = 'tax' GROUP BY ot.order_id)) AS tax, SUM((SELECT SUM(ot.value) FROM `" . DB_PREFIX . "order_total` ot WHERE ot.order_id = o.order_id AND ot.code = 'shipping' GROUP BY ot.order_id)) AS shipping_fee ,SUM(o.total) AS `total` FROM `" . DB_PREFIX . "order` o";
if (!empty($data['filter_order_status_id'])) {
$sql .= " WHERE o.order_status_id = '" . (int)$data['filter_order_status_id'] . "'";
} else {
$sql .= " WHERE o.order_status_id > '0'";
}
if (!empty($data['filter_date_start'])) {
$sql .= " AND DATE(o.date_added) >= '" . $this->db->escape($data['filter_date_start']) . "'";
}
if (!empty($data['filter_date_end'])) {
$sql .= " AND DATE(o.date_added) <= '" . $this->db->escape($data['filter_date_end']) . "'";
}
if (!empty($data['filter_group'])) {
$group = $data['filter_group'];
} else {
$group = 'week';
}
switch($group) {
case 'day';
$sql .= " GROUP BY YEAR(o.date_added), MONTH(o.date_added), DAY(o.date_added)";
break;
default:
case 'week':
$sql .= " GROUP BY YEAR(o.date_added), WEEK(o.date_added)";
break;
case 'month':
$sql .= " GROUP BY YEAR(o.date_added), MONTH(o.date_added)";
break;
case 'year':
$sql .= " GROUP BY YEAR(o.date_added)";
break;
}
$sql .= " ORDER BY o.date_added DESC";
if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}
if ($data['limit'] < 1) {
$data['limit'] = 20;
}
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}
$query = $this->db->query($sql);
return $query->rows;
}
我有一个报告页面,其中的结果根据开关值(如代码所示)按日/月或周分组。
我需要添加一个自定义字段“ REP”,该字段将对PRODUCT表中“ REP_VALUE”下的行的值求和。
我在PRODUCT表中有REP_VALUE。
因此,最终需要在报告下添加此SUM值REP_VALUE。 我尝试了一些操作,但似乎一团糟,代码未返回正确的内容。
这是我的尝试,我在$ sql查询中添加了以下代码:
SUM((SELECT
SUM(myma_product.rep_value)
FROM
myma_product,
myma_order_product
WHERE
myma_product.product_id = myma_order_product.product_id)) as rep_values
但是结果不是我要找的,结果不正确。
答案 0 :(得分:0)
尝试了其他一些请求,但仍然失败。