简体   繁体   中英

MYSQL- Error #1054

After inserting the coding that mentioned in the link previously, I noticed that certain values in the Total_Price from ORDER table is still zero. This is probably because there are some values in the Total_Price from DRINK_ORDER table may be null. I tried to use the coding mentioned below and it gave me "#1054 - Unknown column 'FOOD_ORDER.Order_ID' in 'where clause' ". Can anyone please help me?

By the way ORDER .Total_Price = FOOD_ORDER .Total Price + DRINKS_ORDER .Total_PRICE (This is for reference)

Food_Order表

Drink_Order表

订单表

Previous work

UPDATE `ORDER`
    SET Total_Price = SUM (food_order.Total_Price)  
    WHERE FOOD_ORDER.Order_ID =`ORDER`.Order_ID
    AND `ORDER`.Total_Price = 0;

Schema:

create table `order` -- not a great name by the way
(   Order_Id int auto_increment primary key,
    Total_Price decimal (10,2) not null
);

create table food_order
(   id int auto_increment primary key,
    Order_Id int not null,
    Total_Price decimal(10,2) not null, -- line item total price
    key(Order_Id), -- make it fast
    -- FK below
    CONSTRAINT fk_fo_toOrder FOREIGN KEY (Order_Id) REFERENCES `order`(Order_Id) 
);

create table drink_order
(   id int auto_increment primary key,
    Order_Id int not null,
    Total_Price decimal(10,2) not null, -- line item total price
    key(Order_Id), -- make it fast
    -- FK below
    CONSTRAINT fk_do_toOrder FOREIGN KEY (Order_Id) REFERENCES `order`(Order_Id) 
);

insert `order`(total_price) values (0),(0),(0); -- 3 orders
insert food_order(Order_Id,Total_Price) values (1,1.11),(1,2.22),(1,4.44),(2,20),(2,11.11);
insert drink_order(Order_Id,Total_Price) values (2,22.22),(2,33.33),(3,1000),(3,2000);

The Query (individually run by Order_Id as they occur)

update `Order`
join
(
select o.Order_Id,food.foodSum,drinks.drinkSum
from `order` o
cross join
( select ifnull(sum(Total_Price),0) as foodSum
  from food_order
  where Order_Id=1
) food
cross join 
( select ifnull(sum(Total_Price),0) as drinkSum
  from drink_order
  where Order_Id=1
) drinks
where o.Order_id=1
) inr
on inr.Order_id=`Order`.Order_Id
set `Order`.Total_Price=(inr.foodSum+inr.drinkSum);

The results:

select * from `order`;
+----------+-------------+
| Order_Id | Total_Price |
+----------+-------------+
|        1 |        7.77 |
|        2 |       86.66 |
|        3 |     3000.00 |
+----------+-------------+

Note, the Order_Id could be placed in with a prepared statement, or a stored proc could take a parameter, and wedge it in that way.

The cross join specifies that there is no join clause. Typically thought of as a cartesian product , but in this case 1*1*1 = 1 row resulting.

So it is a join with update pattern , against 1 row.

As for the data, Order 1 had no drinks, Order 3 had no food, Order 2 had both. I think Order 3 had a lot of fun.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM