2015-04-16 46 views
0

我有一個查詢,給我一個價格的產品數量的輸出。 我試圖用ORDER BY p.price ASC LIMIT對它們進行排序,但這並沒有給我正確的排序。 我也試圖改變這種($query->rows as $product),但沒有鍛鍊順序按價格和限制3

這是我的查詢:

$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; 
} 
+0

你'ORDER BY p.price ASC'代碼應工作。有關您獲得的結果的更多信息將有所幫助。 – billynoah

回答

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");