2015-06-20 72 views
0

我有MySQL数据库我想如果更新stock.qty + empiresobjects.food如果stock.product=objects.consumableobjects.product=stock.productstock.qty-objects.consume只有stock.qty>objects.consumeSQL更新多表多条件

CREATE TABLE `objects` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
`name` varchar(45) CHARACTER SET utf8 NOT NULL, 
`period` smallint(5) unsigned NOT NULL, 
`consumedgold` decimal(20,0) unsigned NOT NULL, 
`food` bigint(30) NOT NULL, 
`type` varchar(10) CHARACTER SET utf8 NOT NULL, 
`class` varchar(15) CHARACTER SET utf8 NOT NULL, 
`required` varchar(250) CHARACTER SET utf8 NOT NULL, 
`param0` int(10) unsigned NOT NULL DEFAULT '0', 
`param1` int(10) unsigned NOT NULL DEFAULT '0', 
`param2` int(10) unsigned NOT NULL DEFAULT '0', 
`param3` varchar(12) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0', 
`duplicated` tinyint(3) unsigned NOT NULL DEFAULT '1', 
`upgradable` tinyint(3) unsigned NOT NULL DEFAULT '1', 
`growth` decimal(9,5) NOT NULL DEFAULT '0.00000', 
`objorder` smallint(6) NOT NULL DEFAULT '0', 
`productive` tinyint(3) NOT NULL DEFAULT '0', 
`consumable` varchar(25) COLLATE utf8_unicode_ci NOT NULL, 
`product` varchar(25) COLLATE utf8_unicode_ci NOT NULL, 
`consume` int(10) NOT NULL, 
PRIMARY KEY (`id`), 
KEY `name` (`name`), 
KEY `type` (`type`), 
KEY `class` (`class`), 
KEY `food` (`food`), 
KEY `product` (`product`), 
KEY `productive` (`productive`), 
KEY `consumable` (`consumable`), 
KEY `consume` (`consume`) 
) ENGINE=InnoDB AUTO_INCREMENT=246 DEFAULT CHARSET=utf8   
COLLATE =utf8_unicode_ci; 

empiresobjects

CREATE TABLE `empiresobjects` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
`empireid` int(10) unsigned NOT NULL, 
`starts` int(7) NOT NULL, 
`ends` int(7) NOT NULL, 
`status` tinyint(3) unsigned NOT NULL DEFAULT '0', 
`objectid` int(10) unsigned NOT NULL, 
`level` tinyint(3) unsigned NOT NULL DEFAULT '1', 
`x` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.00', 
`y` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.00', 
`r` smallint(5) DEFAULT '0', 
`z` varchar(20) COLLATE utf8_unicode_ci NOT NULL, 
PRIMARY KEY (`id`), 
KEY `starts` (`starts`), 
KEY `ends` (`ends`), 
KEY `status` (`status`), 
KEY `FK_cityid` (`empireid`), 
KEY `FK_objectid` (`objectid`), 
KEY `level` (`level`) USING BTREE, 
CONSTRAINT `FK_objectid` FOREIGN KEY (`objectid`) REFERENCES `objects` (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=461 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

股票

CREATE TABLE `stock` (
`id` int(11) NOT NULL AUTO_INCREMENT, 
`empireid` int(10) DEFAULT NULL, 
`product` varchar(25) DEFAULT NULL, 
`qty` int(10) DEFAULT NULL, 
PRIMARY KEY (`id`), 
KEY `product` (`product`), 
KEY `qty` (`qty`), 
KEY `empireid` (`empireid`) 
) ENGINE=InnoDB AUTO_INCREMENT=125 DEFAULT CHARSET=utf8mb4; 

iu SE 2更新语句:

update stock s 
,(SELECT sum(objects.consume*empiresobjects.level) as food,objects.consumable,empiresobjects.empireid FROM empiresobjects,objects where status=1 and objects.productive=1 and empiresobjects.objectid=objects.id group by empiresobjects.empireid,objects.consumable) ef 
,(select sum(objects.param0) as size,empiresobjects.empireid from objects,empiresobjects where objects.id=empiresobjects.objectid and objects.class='stock' group by empiresobjects.empireid) es 
,(select sum(objects.food*empiresobjects.level) as food,empiresobjects.empireid from objects,empiresobjects where objects.id=empiresobjects.objectid group by empiresobjects.empireid) etf 
,(select sum(qty) as qty,empireid from stock group by empireid) ets 
set s.qty=s.qty-ef.food 
where s.qty>ef.food and es.size>etf.food+ets.qty and s.product=ef.consumable and s.empireid=ef.empireid and s.empireid=es.empireid and s.empireid=etf.empireid and s.empireid=ets.empireid; 

update stock s 
,(SELECT sum(objects.food*empiresobjects.level) as food,sum(objects.consume*empiresobjects.level) as consume,objects.product,objects.consumable,empiresobjects.empireid FROM empiresobjects,objects where status=1 and objects.productive=1 and empiresobjects.objectid=objects.id group by empiresobjects.empireid,objects.product) ef 
,(select sum(objects.param0) as size,empiresobjects.empireid from objects,empiresobjects where objects.id=empiresobjects.objectid and objects.class='stock' group by empiresobjects.empireid) es 
,(select sum(objects.food*empiresobjects.level) as food,empiresobjects.empireid from objects,empiresobjects where objects.id=empiresobjects.objectid group by empiresobjects.empireid) etf 
,(select sum(qty) as qty,empireid from stock group by empireid) ets 
,(select qty as qty,product,empireid from stock) ec 
set s.qty=s.qty+ef.food 
where ec.empireid=s.empireid and ef.consumable=ec.product and ef.consume<s.qty and es.size>etf.food+ets.qty and s.product=ef.product and s.empireid=ef.empireid and s.empireid=es.empireid and s.empireid=etf.empireid and s.empireid=ets.empireid; 

他们工作,但如果stock.qty>empiresobjects.consume所以我得到负valies如果stock.qty现场不能检查。

+0

您已经显示了您的表格,但未显示您的查询以尝试执行您想要的任务。你可以发布该代码并显示结果吗? –

+0

我编辑了这个问题,并把我的代码 –

回答

0
update stock ps 
,(select stock.empireid,stock.qty,sum(objects.food*empiresobjects.level) as produce,sum(objects.consume*empiresobjects.level) as consume,objects.product,objects.consumable from stock,objects,empiresobjects where stock.empireid=empiresobjects.empireid and objects.id=empiresobjects.objectid and empiresobjects.status=1 and objects.productive=1 and stock.product=objects.consumable group by stock.empireid,objects.product,objects.consumable) d 
,(select sum(objects.param0) as size,empiresobjects.empireid from objects,empiresobjects where objects.id=empiresobjects.objectid and objects.class='stock' group by empiresobjects.empireid) es 
,(select sum(qty) as qty,empireid from stock group by empireid) ets 
set ps.qty=ps.qty-d.consume 
where ps.empireid=d.empireid and ps.empireid=es.empireid and ps.empireid=ets.empireid and ps.product=d.consumable 
and d.qty>d.consume and es.size>ets.qty+d.produce; 

update stock ps 
,(select stock.empireid,stock.qty,sum(objects.food*empiresobjects.level) as produce,sum(objects.consume*empiresobjects.level) as consume,objects.product,objects.consumable from stock,objects,empiresobjects where stock.empireid=empiresobjects.empireid and objects.id=empiresobjects.objectid and empiresobjects.status=1 and objects.productive=1 and stock.product=objects.consumable group by stock.empireid,objects.product,objects.consumable) d 
,(select sum(objects.param0) as size,empiresobjects.empireid from objects,empiresobjects where objects.id=empiresobjects.objectid and objects.class='stock' group by empiresobjects.empireid) es 
,(select sum(qty) as qty,empireid from stock group by empireid) ets 
set ps.qty=ps.qty+d.produce 
where ps.empireid=d.empireid and ps.empireid=es.empireid and ps.empireid=ets.empireid and ps.product=d.product 
and d.qty>d.consume and es.size>ets.qty+d.produce;