更新表错误代码1054

时间:2018-05-16 02:40:39

标签: mysql

我想用我选择的新值更新表ospos_sale值,但无法更新。

这是我的sql代码

update ospos_sales set subtotal_amount = ( select * from (select sale.subtotal_amount-saler.subtotal_amount from ospos_sales sale left join ospos_salesreturn saler on sale.id = saler.fk_sales_id where sale.id=ospos_sales.id) as m2), total_discount = (select * from (select sale.total_discount-saler.total_discount from ospos_sales sale left join ospos_salesreturn saler on sale.id = saler.fk_sales_id where sale.id=ospos_sales.id) as m3), total_amount = (select * from (select sale.total_amount-saler.total_amount from ospos_sales sale left join ospos_salesreturn saler on sale.id = saler.fk_sales_id where sale.id=ospos_sales.id) as m4),change_amount = (select * from (select sale.paid_amount - sale.total_amount+saler.total_amount from ospos_sales sale left join ospos_salesreturn saler on sale.id = saler.fk_sales_id where sale.id=ospos_sales.id) as m4) where ospos_sales.id= 10003

这是我的表结构

    CREATE TABLE `ospos_sales`  (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `saletime` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `fk_customers_id` int(10) NULL DEFAULT 0,
  `fk_users_id` int(10) NOT NULL DEFAULT 0,
  `fk_locations_id` int(255) NOT NULL,
  `comment` text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `invoicenumber` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `quotenumber` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `totalsaledquantity` decimal(15, 3) NOT NULL,
  `subtotal_amount` decimal(25, 0) NOT NULL,
  `fk_dinnertables_id` int(11) NULL DEFAULT NULL,
  `total_discount` decimal(25, 0) NOT NULL,
  `coupon_amount` decimal(25, 0) NOT NULL,
  `tax_amount` decimal(25, 0) NOT NULL,
  `round_amount` decimal(25, 0) NOT NULL,
  `total_amount` decimal(25, 0) NOT NULL,
  `paid_amount` decimal(25, 0) NOT NULL,
  `change_amount` decimal(25, 0) NOT NULL,
  `salestatus` enum('draft','open','sale','return_request','returned','cancel_request','cancelled','cleared') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'draft',
  `paymentstatus` enum('unpaid','paid','partially','return') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'unpaid',
.
.
.
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `invoice_number`(`invoicenumber`) USING BTREE,
  INDEX `customer_id`(`fk_customers_id`) USING BTREE,
  INDEX `employee_id`(`fk_users_id`) USING BTREE,
  INDEX `sale_time`(`saletime`) USING BTREE,
  INDEX `dinner_table_id`(`fk_dinnertables_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 36096 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
SET FOREIGN_KEY_CHECKS = 1;

和表SaleReturnitem是

CREATE TABLE `ospos_salesreturn`  (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `returntime` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `fk_customers_id` int(10) NULL DEFAULT NULL,
  `fk_locations_id` int(255) NOT NULL,
  `fk_sales_id` int(255) NOT NULL,
  `returnref` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `comment` text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `subtotal_amount` decimal(25, 0) NOT NULL,
  `total_discount` decimal(25, 0) NOT NULL,
  `coupon_amount` decimal(25, 0) NOT NULL,
  `tax_amount` decimal(25, 0) NOT NULL,
  `total_amount` decimal(25, 0) NOT NULL,
  `paid_amount` decimal(25, 0) NOT NULL,
  `change_amount` decimal(25, 0) NOT NULL,
  `returnstatus` enum('draft','returned') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'draft',
  `fk_users_id` int(10) NOT NULL DEFAULT 0,
  `status` enum('0','1') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '1',
  `dels` enum('1','0') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0',
  `cdate` int(15) NOT NULL,
  `mdate` int(15) NOT NULL,
  `syncfrom` int(11) NOT NULL DEFAULT 0,
  `syncmasterid` int(255) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `customer_id`(`fk_customers_id`) USING BTREE,
  INDEX `employee_id`(`fk_users_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

错误消息是:

> 1054 - Unknown column 'ospos_sales.id' in 'where clause' 

请帮忙。

2 个答案:

答案 0 :(得分:1)

您的子查询为ospos_sales提供别名。当您为表提供别名时,您不能再使用表名引用它,但必须使用该别名。

我认为你的目标是让它引用更新表,但是在2级中,它看不到它。

最简单的解决方案是简单地将ID放在子查询中,而不是引用列。否则,您需要重新构建查询。

如果我没有错过任何东西,那就是这样的。

update ospos_sales 
set 
    subtotal_amount = (select 
            *
        from
            (select 
                sale.subtotal_amount - saler.subtotal_amount
            from
                ospos_sales sale
            left join ospos_salesreturn saler ON sale.id = saler.fk_sales_id
            where
                sale.id = 10003) as m2),
    total_discount = (select 
            *
        from
            (select 
                sale.total_discount - saler.total_discount
            from
                ospos_sales sale
            left join ospos_salesreturn saler ON sale.id = saler.fk_sales_id
            where
                sale.id = 10003) as m3),
    total_amount = (select 
            *
        from
            (select 
                sale.total_amount - saler.total_amount
            from
                ospos_sales sale
            left join ospos_salesreturn saler ON sale.id = saler.fk_sales_id
            where
                sale.id = 10003) as m4),
    change_amount = (select 
            *
        from
            (select 
                sale.paid_amount - sale.total_amount + saler.total_amount
            from
                ospos_sales sale
            left join ospos_salesreturn saler ON sale.id = saler.fk_sales_id
            where
                sale.id = 10003) as m4)
where
    ospos_sales.id = 10003;

答案 1 :(得分:0)

非常感谢@Thomas帮助我解决了这个问题 现在我有一个解决方案来修改我的查询,现在我的解决方案就在这里

update ospos_sales up set 
subtotal_amount = 
( select a.subtotal_amount from (select sale.id, COALESCE(sale.subtotal_amount,0)-COALESCE(saler.subtotal_amount,0) as subtotal_amount 
from ospos_sales sale left join ospos_salesreturn saler ON sale.id = saler.fk_sales_id
) as a where a.id = up.id
),

total_discount = 
 (select a.total_discount from (select sale.id,COALESCE(sale.total_discount,0)-COALESCE(saler.total_discount,0) as total_discount
  from ospos_sales sale left join ospos_salesreturn saler ON sale.id = saler.fk_sales_id
  ) as a where a.id = up.id
    ),

total_amount = 
 (select a.total_amount from (select sale.id, COALESCE(sale.total_amount,0)-COALESCE(saler.total_amount,0) as total_amount
from ospos_sales sale left join ospos_salesreturn saler ON sale.id = saler.fk_sales_id 
) as a where a.id = up.id
),
change_amount =
 (select a.change_amount from (select sale.id, coalesce(sale.paid_amount,0) - coalesce(sale.total_amount,0) + coalesce(saler.total_amount,0) as change_amount
from ospos_sales sale left join ospos_salesreturn saler ON sale.id = saler.fk_sales_id
) as a where a.id = up.id)
where up.id in (select a.fk_sales_id from ospos_salereturns a )