在我看来,这真的是一个简单的修复,但我无法弄清楚。这是我正在研究的问题:
这是我的代码:
CREATE VIEW order_item_products AS
SELECT o.order_id, o.order_date, o.tax_amount, o.ship_date,
oi.item_price, oi.discount_amount, oi.quantity,
(oi.item_price - oi.discount_amount) AS actual_price,
(actual_price * oi.quantity) AS final_price,
p.product_name
FROM orders o
JOIN order_items oi on o.order_id = oi.order_id
JOIN products p ON p.product_id = oi.product_ID;
我在字段列表中收到错误消息Unknown column“actual_price”。我究竟做错了什么?它不会计算最终价格。我删除了最终的价格声明并执行了我的查询,因此它允许它成为一个列。
感谢任何帮助。
答案 0 :(得分:0)
您尝试在实际存在之前使用别名。我认为这应该有效:
CREATE VIEW order_item_products AS
SELECT
o.order_id,
o.order_date,
o.tax_amount,
o.ship_date,
oi.item_price,
oi.discount_amount,
oi.quantity,
(oi.item_price - oi.discount_amount) AS actual_price,
((oi.item_price - oi.discount_amount) * oi.quantity) AS final_price,
p.product_name
FROM orders AS o
JOIN order_items AS oi
ON o.order_id = oi.order_id
JOIN products AS p
ON p.product_id = oi.product_ID
;
答案 1 :(得分:0)
我的版本似乎有效:
CREATE or REPLACE view order_item_product
AS SELECT o.order_id,o.order_date,o.tax_amount,o.ship_date,
oi.item_price,oi.discount_amount,oi.item_price-oi.discount_amount AS final_price,oi.quantity,COUNT(oi.item_id) AS item_total,
p.product_name
FROM orders o,orderitems oi,product p
WHERE o.order_id=oi.order_id
AND p. product_id=oi. product_id;