Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果我连接两个表,Mysql更新查询限制不起作用_Mysql_Sql Update - Fatal编程技术网

如果我连接两个表,Mysql更新查询限制不起作用

如果我连接两个表,Mysql更新查询限制不起作用,mysql,sql-update,Mysql,Sql Update,在MYSQL中,如果我在连接两个表时在更新中设置了限制,它会显示 错误1221(HY000):更新和限制的使用不正确 我的表架构是 订单详情表 CREATE TABLE `order_details` ( `order_id` int(5) NOT NULL AUTO_INCREMENT, `amount` int(5) DEFAULT NULL, `order_status` char(1) DEFAULT 'N', `company_order_id` int(5) DEFA

在MYSQL中,如果我在连接两个表时在更新中设置了限制,它会显示

错误1221(HY000):更新和限制的使用不正确

我的表架构是
订单详情表

CREATE TABLE `order_details` (
  `order_id` int(5) NOT NULL AUTO_INCREMENT,
  `amount` int(5) DEFAULT NULL,
  `order_status` char(1) DEFAULT 'N',
  `company_order_id` int(5) DEFAULT NULL,
  PRIMARY KEY (`order_id`)
)
产品表

CREATE TABLE `product` (
  `product_id` int(5) NOT NULL AUTO_INCREMENT,
  `product_name` varchar(50) DEFAULT NULL,
  `product_amount` int(5) DEFAULT NULL,
  `product_status` char(1) DEFAULT 'N',
  `company_product_id` int(5) DEFAULT NULL,
  PRIMARY KEY (`product_id`)
)
订单\产品\映射

CREATE TABLE `order_product_mapping` (
  `product_id` int(5) DEFAULT NULL,
  `order_id` int(5) DEFAULT NULL,
  `fulfillment` char(1) DEFAULT 'N'
)

我将获取company_order_id和company_product_id作为输入,以便在order_product_映射中进行更新查询以更改我的履行状态。如果在查询中添加限制,则显示错误

有关错误的解释,请参阅本文:

我确实觉得我找到了一个解决方法。将order_product_映射表自身连接起来,并创建一个行号。使用row number=1的工作方式应与限制1相同:

UPDATE order_product_mapping opm
  JOIN (SELECT *, @rowNum:=@rowNum+1 rn FROM order_product_mapping JOIN (SELECT @rowNum:= 0) r)
    as opm2 ON opm.product_id = opm2.product_id and opm.order_id = opm2.order_id
  JOIN product p ON p.product_id=opm.product_id AND p.product_id=1
  JOIN order_details as od ON od.order_id=opm.order_id 
SET opm.fulfillment = 'Y'
where od.order_id=100 
And rn = 1;

还有一些提琴样品:

谢谢@sgedes。那些链接真的帮了我很多
UPDATE order_product_mapping opm
  JOIN (SELECT *, @rowNum:=@rowNum+1 rn FROM order_product_mapping JOIN (SELECT @rowNum:= 0) r)
    as opm2 ON opm.product_id = opm2.product_id and opm.order_id = opm2.order_id
  JOIN product p ON p.product_id=opm.product_id AND p.product_id=1
  JOIN order_details as od ON od.order_id=opm.order_id 
SET opm.fulfillment = 'Y'
where od.order_id=100 
And rn = 1;