Mysql 更新表错误代码1054

Mysql 更新表错误代码1054,mysql,Mysql,我想用我选择的新值更新表ospos_sale value,但它无法更新 这是我的sql代码 更新ospos\u销售集小计\u金额=从select sale.subtotal\u amount-saler.subtotal\u amount中选择* 从ospos_sales sales left加入ospos_sales Return saler on sale.id=saler.fk_sales_id where sale.id=ospos_sales.id作为m2,总折扣=select*fro

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

这是我的sql代码

更新ospos\u销售集小计\u金额=从select sale.subtotal\u amount-saler.subtotal\u amount中选择* 从ospos_sales sales left加入ospos_sales Return saler on sale.id=saler.fk_sales_id where sale.id=ospos_sales.id作为m2,总折扣=select*from select sale.total_折扣-saler.total_折扣从ospos_sales sales left加入ospos_sales Return sales saler.id=saler.fk_sales_id where sales.id=ospos_sales.id作为m3,合计金额=从选择销售中选择*。合计金额-销售额。合计金额 从ospos_sales sale left加入ospos_sales return saler on sale.id=saler.fk_sales_id其中sale.id=ospos_sales.id为m4,change_amount=select*from select sale.paid_amount-sale.total_amount+saler.total_amount from ospos_sales left join ospos_sales return saler on sale.id=saler.fk_sales_id where sale.id=ospos_sales.id=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;
表中的项目是

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' 

请提供帮助。

您的子查询为ospos_销售人员提供了一个别名。为表指定别名时,不能再使用表名引用它,但必须使用该别名

我假设您的目标是让它引用更新表,但由于在中有两个级别,它无法看到它

最简单的解决方案是将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;

非常感谢@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 )

表ospos_sales是否有一个名为id的列?在没有看到您的数据库的情况下,这似乎非常简单。您不明白错误消息的哪一部分?@Thomas yes,我的表中有一列名为id@GordonLinoff我不明白为什么它说我没有列名id,但在我的表中已经有了这个列如果你愿意给我们看的话,你可以做一个展示,创建ospos_销售表;向我们显示该表中的列以及可能失败的原因。请建议查询结构。因为我不想把身份证号码改成子查询为什么不?假设您使用的是准备好的语句,它应该与您已经添加到查询中的ID一起流动。因为我想这样做,ospos_sales.ID中的ospos_sales.ID从ospos_sales中选择fk_sale_ID返回非常感谢您,我很久以前就忘记了。对此我很抱歉