当我尝试运行这些查询时
Select * from products p, products_to_categories pc
where p.product_id = pc.product_id and pc.category_id ='.$category_id.'
where p.status=1
获得以下错误
您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第1行的'where p.status = 1'附近使用正确的语法
答案 0 :(得分:1)
对于其他条件,请使用and
/ or
代替其他where
。
Select *
from products p
inner join products_to_categories pc on p.product_id = pc.product_id
where pc.category_id = '.$category_id.'
and p.status = 1
此外,您可以使用连接语法。
答案 1 :(得分:0)
您只能使用一次。然后应该跟着AND
你的代码:
WHERE p.product_id = pc.product_id AND pc.category_id ='.$category_id.' AND p.status=1
答案 2 :(得分:0)
WHERE子句只包含一个WHERE后跟连接条件(通过使用AND,OR和括号连接以强制所述条件之间的优先级)。
WHERE p.product_id = pc.product_id
AND pc.category_id = '.$category_id.'
AND p.status = 1
答案 3 :(得分:0)
Select * from products p
inner join products_to_categories pc
on p.product_id = pc.product_id
where pc.category_id ='.$category_id.'
and p.status=1
为了获得更多的语句性能(特别是如果你有很多行),我建议你使用JOIN
子句。它是最快的方法,也是对你的更容易阅读。
选择通常使用关键字AND
,OR
您应该阅读this article。