老菜鸟对 SQL 不熟,不懂就问 -_-
手上有个表格存放一些游戏数据属性:AGI 敏捷。HP 生命血量。MP 魔法能量。
Hero 是英雄 id,toys 是宠物 id 。
Hero toys HP MP AGI A 6 5 2 6 A 4 4 3 5 A 2 7 4 8 A 3 6 4 9 B 5 5 2 6 B 1 5 2 6 B 2 5 2 6 B 3 5 2 4 .......
我想 select
哪些英雄同时拥有宠物 id 2
, 3
,并且在他们拥有这些宠物的时候,他们的 HP 和 AGI 都大于 5
?
比如上面的表格就返回 A
。 (因为 B
虽然同时拥有 2
和3
,但是在3
的时候 AGI<5
)
![]() | 1 wushigejiajia01 2020-10-11 10:33:53 +08:00 via Android ![]() 我也疑惑了 select hero from table where toys in(2,3) and hp>5 and agi>5 啊,不是这样吗? |
![]() | 2 qinxi 2020-10-11 10:33:58 +08:00 ![]() where toys in (2,3) and hp > 5 and agi >5 |
![]() | 3 wangyanrui 2020-10-11 10:38:17 +08:00 via Android 两个 in 的查询 |
![]() | 4 wangyanrui 2020-10-11 10:42:45 +08:00 via Android ![]() in 完直接 distinct hero 楼主是同时满足要有 2 3 宠 select distinct(hero) from table where hero in(2 的查询) and hero in(3 的查询) hp>5 and agi>5 |
![]() | 5 fyooo OP @wushigejiajia01 > select distinct(hero) from table where toys in(2,3) and hp>5 and agi>5 我用这个查询似乎只能查到宠物是 2 或 3,但是不能查到同时拥有 2 和 3 的 |
![]() | 6 fyooo OP @wangyanrui 谢谢哈,实测可行:) |
![]() | 7 wushigejiajia01 2020-10-11 12:01:08 +08:00 @fyooo 嗯嗯, 我的锅, 审题不全 |
![]() | 8 9LCRwvU14033RHJo 2020-10-11 12:09:53 +08:00 ![]() @fyooo 这样不可行不行? select Hero from table_name where toys in (2, 3) and hp > 5 and agi > 5 group by Hero having count(distinct toys) = 2 |
![]() | 10 9LCRwvU14033RHJo 2020-10-11 19:38:58 +08:00 你指的是什么呢?下面这个例子里 C 并不会被选出来呀。 www.db-fiddle.com/f/8rhX2qJheqvz1Gg2t7oasc/0 |