我想请求帮助和UPDATE命令,这取决于4个表的关系。 (或者甚至可能吗?
这是我的桌子:Characters
guid, ...., totalItemlevel
template_items
id, ... itemlevel, ...,
instances_item
itemInstanceID, itemID, Owner
(itemID is foreign key template_items.id, Owner is foreign key Character.guid)
inventory_character
guid, slot, iteminstance
(guid is foreign key character.guid, item is foreign key instances_item.itemInstanceID)
现在我需要做一个UPDATE脚本,它将totalItemLevel设置为template_items.itemLevel的总和,如果角色在库存中具有插槽> 20
的项目的实例在伪陈述中:
UPDATE characters
SET totalItemLevel = SUM(FOR EACH ITEM IN INVENTORY(is slot>20?(GET its template(get itemLevel))));
所以我试过这样的事情
SELECT SUM(template_items.itemLevel)
FROM template_items,
inventory_character,
instances_item,
characters
WHERE chararacter.guid = inventory_character.guid
AND inventory_character.slot > 19
AND inventory_character.iteminstance = instances_item.itemInstanceID
AND instances_item.itemID = template_items.id);
这绝对不行,它很疯狂,它可能需要一些子查询,但我真的不知道,如何实现这一点。
此外,我无法以其他方式执行此操作,例如在PHP中执行某些查询,然后对其进行计数...我需要直接在数据库中执行此更新。
那么请,任何想法,如何实现这个计算列?
提前致谢:)
答案 0 :(得分:0)
假设你需要插槽> 20(在你的帖子中你有一个插槽> 20在一个地方,插槽> 19在另一个地方):
UPDATE Characters
SET Characters.totalItemLevel = s FROM
(
SELECT SUM(template_items.itemlevel) AS s, c.[guid]
FROM Characters c
INNER JOIN inventory_character ON inventory_character.[guid] = c.[guid]
INNER JOIN instances_item ON instances_item.itemInstanceID = inventory_character.iteminstance AND instances_item.[Owner] = c.[guid]
INNER JOIN template_items ON template_items.id = instances_item.itemID
WHERE c.[guid] = instances_item.[Owner] AND slot > 20
GROUP BY c.[guid]
) A
WHERE Characters.[guid] = A.[guid]