好的,我有多个表,我试图基本上形成一个SQL字符串。表格示例:
表1
CardID CardName SetID Rarity
1 Card #1 1 1
2 Card #2 1 2
3 Card #3 1 2
4 Card #4 2 2
表2
SetID SetName BID
1 Set #1 1
2 Set #2 2
表3
BID BName
1 BName #1
2 BName #2
我想要的是一个SQL字符串,它可以计算一组的卡片,并显示该表中每张卡片的稀有性。我想要一个类似于的输出: SetID,SetName,BName,TotalCards,Rarity 1的总和,Rarity 2的总和
因此输出类似于返回列:
1, Set #1, BName #1, 3, 1, 2
2, Set #2, BName #2, 1, 0, 1
答案 0 :(得分:2)
SELECT
c.SetID
, s.SetName
, b.BName
, count(*) as TotalCards
, sum(if(c.rarity = 1,1,0) as Total_of_Rarity_1
, sum(if(c.rarity = 2,1,0) as Total_of_Rarity_2
FROM card c
INNER JOIN `set` s ON (s.SetID = c.SetID)
INNER JOIN b on (b.BID = s.BID)
GROUP BY c.SetID