字段列表中的列名未知-MySQL

字段列表中的列名未知-MySQL,mysql,views,mysql-error-1054,Mysql,Views,Mysql Error 1054,在我看来,这真的是一个简单的解决办法,但我不明白。以下是我正在研究的问题: 创建名为order\u item\u products的视图,该视图从Orders、order\u Items和products表返回列。 此视图应返回订单表中的以下列:订单id、订单日期、税额和发货日期。 此视图应返回订单项目表中的以下列:项目价格、折扣金额、最终价格(从项目价格中减去的折扣金额)、数量和项目总额(项目的计算总额)。 此视图应返回Products表中的product_name列 这是我的代码:

在我看来,这真的是一个简单的解决办法,但我不明白。以下是我正在研究的问题:

  • 创建名为order\u item\u products的视图,该视图从Orders、order\u Items和products表返回列。 此视图应返回订单表中的以下列:订单id、订单日期、税额和发货日期。 此视图应返回订单项目表中的以下列:项目价格、折扣金额、最终价格(从项目价格中减去的折扣金额)、数量和项目总额(项目的计算总额)。 此视图应返回Products表中的product_name列
  • 这是我的代码:

        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;
    
    我在字段列表中收到未知列“实际价格”的错误消息。我做错了什么?它不会计算最终价格。我删除了最终的price语句并执行了查询,因此它允许它成为一个列


    感谢您的帮助

    您试图在别名实际存在之前使用它。我认为这应该奏效:

    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
    ;
    

    我的版本似乎有效:

    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;
    

    不能在定义别名的同时使用别名。因此,
    (实际价格*oi数量)
    必须是
    ((oi.item价格-oi.折扣金额)*oi.quantity)
    谢谢!我知道这很简单。