php - mysql查询错误opencart过滤价格

标签 php mysql opencart opencart2.x

我正在为 opencart 2.0 创建过滤器模块 这是我的代码:
模型/目录/product.php
函数 getProducts()

................................... SOME CODES.............
            //Filter products based on slider price range

            if ((isset($this->request->get['lower']))&&(isset($this->request->get['higher'])))
            {
            $sql .=  " AND p.price >='". $this->request->get['lower'] ." ' AND p.price <='". $this->request->get['higher'] ."'" ;
            }

            //Filter products based on price slider

        if (!empty($data['filter_manufacturer_id'])) {
            $sql .= " AND p.manufacturer_id = '" . (int)$data['filter_manufacturer_id'] . "'";
        }

这对于没有特殊功能的产品非常有用。 但我正在尝试将此代码用于特殊用途。 所以我将我的 SQL 查询更改为:

    if ((isset($this->request->get['lower']))&&(isset($this->request->get['higher'])))
    {
    $sql .=  " AND (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END) >='". $this->request->get['lower'] ." ' AND (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END) <='". $this->request->get['higher'] ."'" ;
    }

但它不适用于具有特殊性的产品。

我收到此错误:“where 子句”中的未知列“special”

最佳答案

在sql的WHERE语句前添加如下内容

LEFT JOIN " . DB_PREFIX . "product_special p2s ON p.product_id = ps.product_id LEFT JOIN " . DB_PREFIX . "product_discount p2d ON p.product_id = pt.product_id

然后在 sql 上使用 'special' 作为 'p2s.special' 和 'discount' 作为 'p2d.discount'

关于php - mysql查询错误opencart过滤价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30853982/

相关文章:

php - 停止在 php 中输出动态 ul 列表中的一项

javascript - Jquery Ajax 无法获取当前聚焦的输入元素

php - Opencart 在自定义脚本中发送电子邮件

mysql - 将mysql值拆分为未知数量的部分

mysql - 无法将现有 sql 文件导入到空数据库 : db_name. table_name 不存在

PHP 通过在 x 个字符后的最后一个空格上剪切字符串来制作摘录

php - 如何通过RESTful Grails驱动的后端对Wordpress前端进行身份验证?

javascript - 如何在 Yii2 中使用 post 请求在 ajax 调用中发送多个参数

php - 根除电子邮件地址的模式

mysql - MySQL数据库中如何按时差去除重复项