这是我的疑问:
SELECT
`i`.`itemtype` AS `Item`,
`p`.`name`
SUM(`i`.`count`) AS `Count`
FROM `player_items` AS `i`
LEFT JOIN `players` AS `p` ON (`p`.`id` = `i`.`player_id`)
WHERE `i`.`itemtype` IN (2148, 2152, 2160)
AND `i`.`player_id` = " . intval ( $main_Char->getId() ) . "
GROUP BY `i`.`itemtype`
LIMIT 0, 30;
是否有可能为其选择的每个项目多次计数?我的意思是WHERE
我.
itemtype IN (2148, 2152, 2160)
它确定了所有ID项的计数:2148, 2152, 2160
我需要它来做同样的事情,但另外要增加每个项目的计数:2148 * 10
,2152 * 1000
,2160 * 10000
和然后在Count
行显示结果。
答案 0 :(得分:1)
是的,您可以一次SUM
多个列:
SELECT i.itemtype AS Item
, p.name
, SUM(CASE
WHEN itemtype = 2148 THEN i.count
ELSE 0
END) * 10 AS Count
, SUM(CASE
WHEN itemtype = 2152 THEN i.count
ELSE 0
END) * 1000 AS Count1
, SUM(CASE
WHEN itemtype = 2160 THEN i.count
ELSE 0
END) * 10000 AS Count2
FROM player_items AS i
LEFT JOIN players AS p ON i.player_id = p.id
WHERE i.itemtype IN (2148, 2152, 2160)
GROUP BY i.itemtype ASC
LIMIT 0, 30
要将这三列合并在一起,您可以:
SELECT t.itemType AS Item, t.Count, t.Count1, t.Count2
, SUM(t.Count1, t.Count2, t.Count3) AS Total
FROM
(
SELECT i.itemtype AS Item
, p.name
, SUM(CASE
WHEN itemtype = 2148 THEN i.count
ELSE 0
END) * 10 AS Count
, SUM(CASE
WHEN itemtype = 2152 THEN i.count
ELSE 0
END) * 1000 AS Count1
, SUM(CASE
WHEN itemtype = 2160 THEN i.count
ELSE 0
END) * 10000 AS Count2
FROM player_items AS i
LEFT JOIN players AS p ON i.player_id = p.id
WHERE i.itemtype IN (2148, 2152, 2160)
GROUP BY i.itemtype ASC
LIMIT 0, 30
) AS t
或者您可以添加另一个CASE
SELECT i.itemtype AS Item
, p.name
, SUM(CASE
WHEN itemtype = 2148 THEN i.count
ELSE 0
END) * 10 AS Count
, SUM(CASE
WHEN itemtype = 2152 THEN i.count
ELSE 0
END) * 1000 AS Count1
, SUM(CASE
WHEN itemtype = 2160 THEN i.count
ELSE 0
END) * 10000 AS Count2
, SUM(CASE
WHEN itemtype = 2148 THEN (i.count * 10)
WHEN itemtype = 2152 THEN (i.count * 1000)
WHEN itemtype = 2160 THEN (i.count * 10000)
ELSE 0
END) AS Total
FROM player_items AS i
LEFT JOIN players AS p ON i.player_id = p.id
WHERE i.itemtype IN (2148, 2152, 2160)
GROUP BY i.itemtype ASC
LIMIT 0, 30
答案 1 :(得分:0)
目前还不是很清楚你想要什么。也许这个:
SUM(i.`count`) * COUNT(*) AS `Count`
或者这个:
SUM(i.`count`) * i.itemtype AS `Count`