[英]Using parent field in subquery
I've got the following query on a Prestashop 1.4 DB and I can't figure out how to use parent id_order field in the subquery. 我在Prestashop 1.4 DB上有以下查询,我无法弄清楚如何在子查询中使用父id_order字段。
Error message is "#1054 - 'o.id_order' field unknown in where clause"
. 错误消息是
"#1054 - 'o.id_order' field unknown in where clause"
。
I tried using a left join but getting same error. 我尝试使用左连接但得到相同的错误。
Both tables order and order_detail have an id_order field. 表order和order_detail都有id_order字段。 There can be many order_detail rows for one 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 ...
Is it because I try to use it in a where clause? 是因为我试图在where子句中使用它吗?
On the following query (can't use it on this case), o.id_order is well known... 在下面的查询(在这种情况下不能使用它),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`,
Try using only one subquery: 尝试只使用一个子查询:
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
Because, if you nest two subquerys the most inner one will not be able to reference to the outer one. 因为,如果你嵌套两个subquerys,那么最内层的subquerys将无法引用外部的subquerys。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.