0

假设我的名字是约翰。约翰有 2000 个弹珠。

除了,您可能在一个插槽上只能有 100 个弹珠。他有 20 个插槽,每个插槽 100 个弹珠

如果我要创建一个表说他有 2000 个弹珠,我会怎么做?相反,它显示了这一点:

John - 100
John - 100
John - 100
Becky - 65
Squid - 40

我想这样说:

John - 300
Becky - 65
Squid - 40

这是我到目前为止所拥有的:

SELECT characters.name, inventoryitems.quantity 
FROM characters, inventoryitems, accounts 
WHERE characters.accountid=accounts.id and characters.id=inventoryitems.characterid and inventoryitems.itemid = 4001190 and accounts.banned = 0 
ORDER BY inventoryitems.quantity

帮助将不胜感激。

4

1 回答 1

1

尝试INNER JOIN查询并使用SUM()函数和GROUP BY子句

SELECT a.name, SUM(b.quantity) as total
FROM characters a 
INNER JOIN accounts c
ON a.accountid=c.id
INNER JOIN inventoryitems b 
ON c.id=b.characterid 
WHERE b.itemid = 4001190
AND c.banned = 0 
GROUP BY a.name
ORDER BY total
于 2013-06-16T17:11:27.190 回答