您的SQL语法有错误;检查手册

时间:2013-12-31 10:56:04

标签: php mysql sql

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第4行的'WHERE j。id_customer = 1'附近使用正确的语法

SELECT j.`id_customer`, j.`id_order`, m.`id_shop`
        FROM `ps_orders` j     
        LEFT JOIN `ps_order_detail` m   
        WHERE j.`id_customer` = 1

如果打开详细的错误纠正,这是从prestashop php中的原始代码生成的-------

 foreach ($result as $key)
        {
                        $customer_id_is = 1;
             $product_id_is = 5;
                    $result2 = Db::getInstance()->executeS(
        'SELECT j.`id_customer`, j.`id_order`, m.`id_shop`
        FROM `'._DB_PREFIX_.'orders` j     
        LEFT JOIN `'._DB_PREFIX_.'order_detail` m   
        WHERE j.`id_customer` = '.$customer_id_is.'


          ');  

        }

2 个答案:

答案 0 :(得分:9)

您错过了JOIN

中的ON条款
SELECT j.id_customer, j.id_order, m.id_shop FROM ps_orders j
LEFT JOIN ps_order_detail m
    ON m.SomeField = j.SomeField <-- HERE 
WHERE j.id_customer = 1

如果您要加入表格,则需要在字段上链接它们。否则,您正在执行交叉连接。

MySQL Docs状态:

  

在MySQL中,JOIN,CROSS JOIN和INNER JOIN是语法等价物(它们可以互相替换)。在标准SQL中,它们不等效。 INNER JOIN与ON子句一起使用,否则使用CROSS JOIN。

所以这会奏效:

SELECT j.id_customer, j.id_order, m.id_shop FROM ps_orders j
JOIN ps_order_detail m
WHERE j.id_customer = 1

就像这样:

SELECT j.id_customer, j.id_order, m.id_shop FROM ps_orders j
INNER JOIN ps_order_detail m
WHERE j.id_customer = 1

但它不适用于OUTER JOIN。

答案 1 :(得分:3)

当您链接两个表时,您必须在每个连接它们的表上定义一列。

您在未指定的ON JOIN条件下执行该操作。例如:

SELECT *
FROM table1 
LEFT JOIN table2 ON table1.pk_id = table2.fk_id