【问题标题】:how to use condition to check 2 things and when the 2 are true, it give an information如何使用条件检查两件事,当两件事为真时,它会提供信息
【发布时间】:2021-02-01 00:48:21
【问题描述】:

我做了一个代码来告诉我一个 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剑类。

但是当我在 2 括号之间放置 and 时,它什么也没做。 当我使用or 时,它显示所有人都在第一手使用剑,所以也会出现不使用两把剑的人。

我不知道如何选择玩家姓名和等级一次(因为当他们使用 2 把剑时,他们的名字会出现两次而不是一次),所以检查他们的双手,如果他们双手都有剑,显示他们的名字和等级。

【问题讨论】:

    标签: sql sql-server inner-join where-clause having-clause


    【解决方案1】:

    您要检查属于给定字符的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 条件

    【讨论】:

    • 它给出了这个错误“'equipslot'附近的语法不正确。”
    • @SamyPereger:缺少and... 已修复。
    猜你喜欢
    • 2022-07-22
    • 2023-01-18
    • 1970-01-01
    • 2022-01-02
    • 2014-01-27
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多