MySQL中的DELETE语句不会使表名正确

时间:2014-09-05 09:59:49

标签: mysql sql

每当我在其中运行此查询时,我都有一个名为broodjes-service的数据库:

DELETE FROM order_items WHERE order_id = 42

我收到此错误:

#1146 Table 'broodjes-service.i' doesn't exist

虽然我确定这个表存在,但是这个表结构:

enter image description here

问题

我正在尝试从名为order_items的表中删除一行,但由于某种原因它会占用表名i

问题

  • 为什么要使用表名i
  • 如何才能使我的查询实际上来自该特定表的一行?

表格triggers

我有order_items

这个用于删除内容时:

enter image description here

UPDATE orders o
   INNER JOIN (SELECT i.order_id id, SUM(i.quantity * p.price) total_price
      FROM `orders-items` i 

   INNER JOIN products p ON p.id = i.products_id 
      AND i.order_id = old.order_id) t ON t.id = o.id 

SET o.total_price = t.total_price

插入触发器

enter image description here

INNER JOIN (SELECT i.order_id id, SUM(i.quantity * p.price) total_price 
FROM `orders-items` i

INNER JOIN products p ON p.id = i.products_id AND i.order_id = new.order_id) t ON t.id = o.id 

SET o.total_price = t.total_price

更新触发器

enter image description here

UPDATE orders o INNER JOIN (SELECT i.order_id id, SUM(i.quantity * p.price) total_price FROM `orders-items` i INNER JOIN products p ON p.id = i.products_id AND i.order_id = new.order_id) t ON t.id = o.id SET o.total_price = t.total_price

2 个答案:

答案 0 :(得分:0)

DELETE FROM `broodjes-service`.order_items 
WHERE order_id = 42

如果你使用别名,那就像这样

DELETE i 
FROM `broodjes-service`.order_items i
WHERE order_id = 42

不要混合数据库名称和别名

DELETE `broodjes-service`.i 
FROM `broodjes-service`.order_items i
WHERE order_id = 42

答案 1 :(得分:-1)

尝试:

Use broodjes-service;
DELETE FROM order_items WHERE order_id = 42