感谢您停止我的问题:)
我正在为我的网上商店开发一个Opencart过滤器模块,但遇到了一个有点难以破解的MySql查询。
该脚本的目的是通过给定的过滤器凭据返回找到的产品数量:
我遇到的问题是在设置最低/最高价格时获得正确的产品数量。这是因为有些产品正在销售中。这意味着它应该检查价格是否在特价范围内,而不是正常价格。
产品价格为“p.price”
$query = $this->db->query("SELECT *, count(p.product_id) as count FROM " . DB_PREFIX . "manufacturer m
LEFT JOIN " . DB_PREFIX . "manufacturer_to_store m2s ON (m.manufacturer_id = m2s.manufacturer_id)
LEFT JOIN " . DB_PREFIX . "product p ON (m.manufacturer_id = p.manufacturer_id)
LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p2c.product_id = p.product_id)
LEFT JOIN " . DB_PREFIX . "product_special ps ON (ps.product_id = p.product_id)
WHERE m2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND m.manufacturer_id = '" . $manufacturer_id . "'
AND (ps.price BETWEEN '" . $min_price . "' AND '" . $max_price . "')
AND (p.price BETWEEN '" . $min_price . "' AND '" . $max_price . "')
AND p.status = 1
");
$manufacturer_data = $query->rows;
我需要的是以某种方式只让它从ps.price中选择它是否为null,否则从p.price列
提前致谢:)
答案 0 :(得分:0)
尝试此查询...
$query = $this->db->query("
SELECT *, CASE WHEN ps.price IS NULL THEN p.price ELSE ps.price END product_price
FROM " . DB_PREFIX . "manufacturer m
LEFT JOIN " . DB_PREFIX . "manufacturer_to_store m2s ON (m.manufacturer_id = m2s.manufacturer_id)
LEFT JOIN " . DB_PREFIX . "product p ON (m.manufacturer_id = p.manufacturer_id)
LEFT JOIN " . DB_PREFIX . "product_special ps ON (ps.product_id = p.product_id)
WHERE m2s.store_id = '" . (int)$this->config->get('config_store_id') . "'
AND m.manufacturer_id = '" . $manufacturer_id . "'
AND (product_price BETWEEN '" . $min_price . "' AND '" . $max_price . "')
AND p.status = 1
");