我在Prestashop 1.4数据库上得到了以下查询,但我无法弄清楚如何在子查询中使用父id_order字段。
错误讯息为"#1054 - 'o.id_order' field unknown in where clause"
。
我尝试使用左连接但得到同样的错误。
两个表order和order_detail都有一个id_order字段。一个订单可以有许多order_detail行。
SELECT
o.invoice_date,
o.invoice_number,
(
SELECT ROUND(SUM(price),2) FROM
(
SELECT (od.product_price * od.tax_rate / 100) as `price`
FROM order_detail od
WHERE od.id_order = o.id_order
) as `tmp`
) as `tax`
FROM `order` o
WHERE o.invoice_number != 0 ...
是因为我尝试在where子句中使用它吗?
在下面的查询中(不能在这种情况下使用它),o.id_order是众所周知的......
(
SELECT ROUND(SUM(total_amount),2)
FROM order_detail_tax ot
WHERE ot.id_tax = 2 AND ot.id_order_detail
IN (
SELECT od.id_order_detail
FROM order_detail od
WHERE od.id_order = o.id_order
)
) as `Tax`,
答案 0 :(得分:2)
尝试只使用一个子查询:
SELECT
o.invoice_date,
o.invoice_number,
(
SELECT ROUND(SUM(od.product_price * od.tax_rate / 100), 2)
FROM order_detail od
WHERE od.id_order = o.id_order
) as `tax`
FROM `order` o
WHERE o.invoice_number != 0
因为,如果嵌套两个子查询,则最内部的子查询将无法引用外部子查询。