【问题标题】:MySQL error: check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOINMySQL 错误:检查与您的 MySQL 服务器版本相对应的手册,以在“LEFT JOIN”附近使用正确的语法
【发布时间】:2015-04-21 13:53:57
【问题描述】:

我正在尝试运行查询,但无法获得正确的输出,并且出现此错误,我尝试了一些 stackoverflow,例如。但我无法解决它。

--错误---

注意:错误:您的 SQL 语法有错误;检查手册 对应于您的 MySQL 服务器版本以获得正确的语法 在 'LEFT JOIN oc_product_to_manufacturer p2m ON (p.product_id = p2m.product_id) WHER' 在第 1 行 错误号:1064

SELECT COUNT(DISTINCT p.product_id) AS total FROM oc_product p LEFT JOIN oc_product_description pd ON (p.product_id = pd.product_id) 左连接 oc_product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pd.language_id = '1' AND p.status = '1' AND p.date_available

问题所在的总代码

public function getTotalProducts($data = array()) {
    $sql = "SELECT COUNT(DISTINCT p.product_id) AS total";
    if (!empty($data['filter_category_id'])) {
        if (!empty($data['filter_sub_category'])) {
                            $sql .= " FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (cp.category_id = p2c.category_id)";
                        } else {
                            $sql .= " FROM " . DB_PREFIX . "product_to_category p2c";
                        }

                        if (!empty($data['filter_filter'])) {
                            $sql .= " LEFT JOIN " . DB_PREFIX . "product_filter pf ON (p2c.product_id = pf.product_id) LEFT JOIN " . DB_PREFIX . "product p ON (pf.product_id = p.product_id)";
                        } else {
                            $sql .= " LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id)";
                        }
                    } else {
                        $sql .= " FROM " . DB_PREFIX . "product p";
                    }

                    $sql .= " 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) WHERE 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') . "'";

                    if (!empty($data['filter_category_id'])) {
                        if (!empty($data['filter_sub_category'])) {
                            $sql .= " AND cp.path_id = '" . (int)$data['filter_category_id'] . "'";
                        } else {
                            $sql .= " AND p2c.category_id = '" . (int)$data['filter_category_id'] . "'";
                        }

                        if (!empty($data['filter_filter'])) {
                            $implode = array();

                            $filters = explode(',', $data['filter_filter']);

                            foreach ($filters as $filter_id) {
                                $implode[] = (int)$filter_id;
                            }

                            $sql .= " AND pf.filter_id IN (" . implode(',', $implode) . ")";
                        }
                    }

                    if (!empty($data['filter_name']) || !empty($data['filter_tag'])) {
                        $sql .= " AND (";

                        if (!empty($data['filter_name'])) {
                            $implode = array();

                            $words = explode(' ', trim(preg_replace('/\s+/', ' ', $data['filter_name'])));

                            foreach ($words as $word) {
                                $implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'";
                            }

                            if ($implode) {
                                $sql .= " " . implode(" AND ", $implode) . "";
                            }

                            if (!empty($data['filter_description'])) {
                                $sql .= " OR pd.description LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
                            }
                        }

                        if (!empty($data['filter_name']) && !empty($data['filter_tag'])) {
                            $sql .= " OR ";
                        }

                        if (!empty($data['filter_tag'])) {
                            $sql .= "pd.tag LIKE '%" . $this->db->escape(utf8_strtolower($data['filter_tag'])) . "%'";
                        }

                        if (!empty($data['filter_name'])) {
                            $sql .= " OR LCASE(p.model) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
                            $sql .= " OR LCASE(p.sku) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
                            $sql .= " OR LCASE(p.upc) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
                            $sql .= " OR LCASE(p.ean) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
                            $sql .= " OR LCASE(p.jan) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
                            $sql .= " OR LCASE(p.isbn) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
                            $sql .= " OR LCASE(p.mpn) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
                        }

                        $sql .= ")";
                    }

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

                /*  if (!empty($data['filter_manufacturer_id'])) {
            $sql .= " LEFT JOIN " . DB_PREFIX . "product_to_manufacturer p2m ON (p2m.product_id = p.product_id) WHERE p2m.manufacturer_id = '" . (int)$data['filter_manufacturer_id'] . "'";*/

                    if (!empty($data['filter_manufacturer_id'])) {
            $sql .= " LEFT JOIN " . DB_PREFIX . "product_to_manufacturer p2m ON (p.product_id = p2m.product_id) WHERE p2m.manufacturer_id = '" . (int)$data['filter_manufacturer_id'] . "'";


            }

                    $query = $this->db->query($sql);

                    return $query->row['total'];
                }

这是导致错误的主要代码

if (!empty($data['filter_manufacturer_id'])) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_to_manufacturer p2m 
       ON (p.product_id = p2m.product_id) 
       WHERE p2m.manufacturer_id = '" . (int)$data['filter_manufacturer_id'] . "'";
}

【问题讨论】:

  • echo $sql 并显示实际查询是什么。
  • 你不能在一个 WHERE 后跟另一个 WHERE 进行 LEFT JOIN
  • 你不能在 where 子句之后添加连接,这是有问题的。
  • 这是在 opencart 中,我不知道如何从模型中回显它
  • @wurtel,但我可以在 opencart 的许多地方看到他们使用它而没有出现任何错误,但是当我尝试以同样的方式添加时,我得到了这个错误

标签: php mysql


【解决方案1】:

您在引发错误的行附近混合了 ON 和 WHERE。将您的查询更改为如下所示:

SELECT COUNT(DISTINCT p.product_id) AS total 
FROM oc_product p 
    LEFT JOIN oc_product_description pd ON (p.product_id = pd.product_id) 
    LEFT JOIN oc_product_to_store p2s ON (p.product_id = p2s.product_id) 
        AND pd.language_id = '1' AND p.status = '1' 
        AND p.date_available <= NOW() AND p2s.store_id = '0' 
    LEFT JOIN oc_product_to_manufacturer p2m ON (p.product_id = p2m.product_id) 
        AND p2m.manufacturer_id = '8'

【讨论】:

    猜你喜欢
    • 2011-08-07
    • 2016-08-06
    • 1970-01-01
    • 2012-06-27
    • 2015-03-30
    • 2012-05-02
    • 2013-07-14
    • 1970-01-01
    相关资源
    最近更新 更多