我编写了一个代码来告诉我一个rpg游戏,有多少名玩家正在使用特定的类(所以我也要检查第一手,剑和第二手,剑,并且它应该告诉我任何高于等级的玩家的名称和等级双手握剑100,下面是代码:
declare @level varchar(100)
set @level=80
select level,name from characters c
inner join items i on i.characterId=c.characterId
where
( level>@level AND equipSlot=5 and (
itemId=2002 OR --short sword
itemId=2005 OR --steel rapier
itemId=2010 OR --volt sword
itemid=2012 OR --goblin knife
itemId=2017 OR --crimson sword
itemId=2026 OR --tree splitter sword
itemId=2031 OR --sea king sword
itemId=2034 OR --acrodont blade
itemid=2054 OR --halloween sword
itemId=2059 --stinger
))
and
(level>@level and equipSlot=6 and (
itemId=2002 OR --short sword
itemId=2005 OR --steel rapier
itemId=2010 OR --volt sword
itemid=2012 OR --goblin knife
itemId=2017 OR --crimson sword
itemId=2026 OR --tree splitter sword
itemId=2031 OR --sea king sword
itemId=2034 OR --acrodont blade
itemid=2054 OR --halloween sword
itemId=2059 --stinger
))
ORDER by level desc
因此,我检查了第一手(equipslot = 6),检查是否有一把可用剑,然后如果第二只手(equipslot = 5)也有一把剑,则表示该人正在使用2类剑。
但是当我and
在2括号之间加上一个时,它什么也没做。并且它显示了当我使用时所有人民都亲手使用剑or
,因此不使用2剑的人民也会出现。
我不知道该如何选择玩家名称和等级(因为当他们使用2把剑时,他们的名字会出现两次而不是一次),因此请检查他们的手,以及如果他们手里都有剑,请显示他们的名字和水平。
你要检查组items
属于给定的字符满足条件,这表明聚集:
select c.name
from characters c
inner join items i on i.characterId=c.characterId
where
i.level > @level
and i.equipslot in (5, 6)
and i.itemid in (2002, 2005, 2010, 2012, 2017, 2026, 2031, 2034, 2054, 2059)
group by c.name
having count(distinct equipslot) = 2
如果要显示每只手的水平,可以执行以下操作:
select c.name,
max(case when i.equipslot = 5 then level end) level_hand_5,
max(case when i.equipslot = 6 then level end) level_hand_6
from characters c
inner join items i on i.characterId=c.characterId
where
i.level > @level
and i.equipslot in (5, 6)
and i.itemid in (2002, 2005, 2010, 2012, 2017, 2026, 2031, 2034, 2054, 2059)
group by c.name
having count(distinct equipslot) = 2
笔记:
在多表查询中,最好的做法是在每一列之前添加它所属的表;我做了一些假设,您可能需要复习
in
可以方便地缩短多个or
条件
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句