SQL:从带变量的表中选择

时间:2016-01-04 23:00:14

标签: mysql sql database prestashop

我刚刚开始深入研究SQL,所以这很可能是一个简单的语法错误,但我仍然陷入困境。 我通过语法检查程序执行了此操作(这有多大帮助,我不确定)并且它在第6行返回错误。我正在绘制一个空白,所以任何方向我将非常感激。

我试图回归" o"从表中一次一行满足product_id = 12,以便我找到下一个必需的数据

SELECT 
o.`id_order`,
cs.`id_customer`,
ord.`id_customer`,
concat( cs.`firstname`,' ', cs.`lastname`) as "Customer"

FROM `ps_order_detail` o (WHERE product_id=12)
join `ps_orders` ord on o.`id_order`=ord.`id_order`
join `ps_customer` cs on ord.`id_customer`=cs.`id_customer`

2 个答案:

答案 0 :(得分:3)

您的WHERE子句放错地方,只应在所有JOIN完成后出现,如下所示:

SELECT 
o.`id_order`,
cs.`id_customer`,
ord.`id_customer`,
concat( cs.`firstname`,' ', cs.`lastname`) as "Customer"

FROM `ps_order_detail` o
join `ps_orders` ord on o.`id_order`=ord.`id_order`
join `ps_customer` cs on ord.`id_customer`=cs.`id_customer`

WHERE o.product_id=12

它将具有您想要的相同过滤效果。

答案 1 :(得分:1)

您应该在加入后使用where where条件,同时为Customer

删除2个双重配额
SELECT 
o.`id_order`,
cs.`id_customer`,
ord.`id_customer`,
concat( cs.`firstname`,' ', cs.`lastname`) as Customer

FROM `ps_order_detail` o 
join `ps_orders` ord on o.`id_order`=ord.`id_order`
join `ps_customer` cs on ord.`id_customer`=cs.`id_customer`
WHERE o.product_id=12