按价格和限额订购3

时间:2015-04-16 19:19:59

标签: php sql-order-by opencart

我有一个查询,它给了我一些价格很高的产品。 我尝试用ORDER BY p.price ASC LIMIT对它们进行排序,但这并没有给我正确的排序。 我也尝试改变这个($query->rows as $product),但没有锻炼

这是我的疑问:

$result = array();

if ($this->customer->isLogged()) {
  $customer_group_id = $this->customer->getCustomerGroupId();
} else {
  $customer_group_id = $this->config->get('config_customer_group_id');
}

$query = $this->db->query("SELECT DISTINCT *,
                             pd.name AS name,
                             p.image, m.name AS manufacturer,
                            (SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$customer_group_id . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) AS discount,
                            (SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special,
                            (SELECT points FROM " . DB_PREFIX . "product_reward pr WHERE pr.product_id = p.product_id AND customer_group_id = '" . (int)$customer_group_id . "') AS reward, (SELECT ss.name FROM " . DB_PREFIX . "stock_status ss WHERE ss.stock_status_id = p.stock_status_id AND ss.language_id = '" . (int)$this->config->get('config_language_id') . "') AS stock_status,
                            (SELECT wcd.unit FROM " . DB_PREFIX . "weight_class_description wcd WHERE p.weight_class_id = wcd.weight_class_id AND wcd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS weight_class, (SELECT lcd.unit FROM " . DB_PREFIX . "length_class_description lcd WHERE p.length_class_id = lcd.length_class_id AND lcd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS length_class,
                            (SELECT AVG(rating) AS total FROM " . DB_PREFIX . "review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating,
                            (SELECT COUNT(*) AS total FROM " . DB_PREFIX . "review r2 WHERE r2.product_id = p.product_id AND r2.status = '1' GROUP BY r2.product_id) AS reviews

                          FROM " . DB_PREFIX . "product p 
                          LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) 
                          LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id)
                          LEFT JOIN " . DB_PREFIX . "manufacturer m ON (p.manufacturer_id = m.manufacturer_id)
                          WHERE
                            p.aldoc = '" . $this->db->escape($aldoc_id) . "' AND
                            pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND
                            p.status = '1' AND
                            p.date_available <= NOW() AND
                           p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'");
                         //  ORDER BY p.price ASC LIMIT 3");

foreach ($query->rows as $product){
  $result[] = array(
    'product_id'       => $product['product_id'],
    'name'             => $product['name'],
    'description'      => $product['description'],
    'meta_description' => $product['meta_description'],
    'meta_keyword'     => $product['meta_keyword'],
    'tag'              => $product['tag'],
    'model'            => $product['model'],
    'sku'              => $product['sku'],
    'upc'              => $product['upc'],
    'ean'              => $product['ean'],
    'jan'              => $product['jan'],
    'isbn'             => $product['isbn'],
    'mpn'              => $product['mpn'],
    'location'         => $product['location'],
    'quantity'         => $product['quantity'],
    'stock_status'     => $product['stock_status'],
    'image'            => $product['image'],
    'manufacturer_id'  => $product['manufacturer_id'],
    'manufacturer'     => $product['manufacturer'],
    'price'            => ($product['discount'] ? $product['discount'] : $product['price']),
    'special'          => $product['special'],
    'reward'           => $product['reward'],
    'points'           => $product['points'],
    'tax_class_id'     => $product['tax_class_id'],
    'date_available'   => $product['date_available'],
    'weight'           => $product['weight'],
    'weight_class_id'  => $product['weight_class_id'],
    'length'           => $product['length'],
    'width'            => $product['width'],
    'height'           => $product['height'],
    'length_class_id'  => $product['length_class_id'],
    'subtract'         => $product['subtract'],
    'rating'           => round($product['rating']),
    'reviews'          => $product['reviews'],
    'minimum'          => $product['minimum'],
    'sort_order'       => $product['sort_order'],
    'status'           => $product['status'],
    'date_added'       => $product['date_added'],
    'date_modified'    => $product['date_modified'],
    'viewed'           => $product['viewed']
  );
}

return $result;
}

1 个答案:

答案 0 :(得分:0)

我刚刚完全按原样尝试了您的代码,只省略了p.aldoc = '" . $this->db->escape($aldoc_id) . "',因为我没有该字段,而且它可以正常工作

ORDER BY p.price ASC LIMIT 3");

然而,你当前的结局却没有。你需要做到:

p2s.store_id = '" . (int)$this->config->get('config_store_id') . "');

这两者应该是这样的:

p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'
ORDER BY p.price ASC LIMIT 3");