V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
fyooo
V2EX  ›  MySQL

SQL 怎么查询满足多个属性的数据?

  •  
  •   fyooo · 2020-10-11 10:26:16 +08:00 · 3525 次点击
    这是一个创建于 1290 天前的主题,其中的信息可能已经有所发展或是发生改变。

    老菜鸟对 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 虽然同时拥有 23,但是在3的时候 AGI<5)

    第 1 条附言  ·  2020-10-11 11:06:07 +08:00
    谢谢几位的回复,已经发送感谢:)
    11 条回复    2020-10-12 09:44:22 +08:00
    wushigejiajia01
        1
    wushigejiajia01  
       2020-10-11 10:33:53 +08:00 via Android   ❤️ 1
    我也疑惑了

    select hero from table where toys in(2,3) and hp>5 and agi>5

    啊,不是这样吗?
    qinxi
        2
    qinxi  
       2020-10-11 10:33:58 +08:00   ❤️ 1
    where toys in (2,3) and hp > 5 and agi >5
    wangyanrui
        3
    wangyanrui  
       2020-10-11 10:38:17 +08:00 via Android
    两个 in 的查询
    wangyanrui
        4
    wangyanrui  
       2020-10-11 10:42:45 +08:00 via Android   ❤️ 1
    in 完直接 distinct hero

    楼主是同时满足要有 2 3 宠😬😬



    select distinct(hero)
    from table where
    hero in(2 的查询) and
    hero in(3 的查询)
    hp>5 and
    agi>5
    fyooo
        5
    fyooo  
    OP
       2020-10-11 11:16:11 +08:00
    @wushigejiajia01

    > select distinct(hero) from table where toys in(2,3) and hp>5 and agi>5

    我用这个查询似乎只能查到宠物是 2 或 3,但是不能查到同时拥有 2 和 3 的
    fyooo
        6
    fyooo  
    OP
       2020-10-11 11:23:27 +08:00
    @wangyanrui 谢谢哈,实测可行:)
    wushigejiajia01
        7
    wushigejiajia01  
       2020-10-11 12:01:08 +08:00
    @fyooo
    嗯嗯, 我的锅, 审题不全
    user8341
        8
    user8341  
       2020-10-11 12:09:53 +08:00   ❤️ 1
    @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
    zzz686970
        9
    zzz686970  
       2020-10-11 19:09:39 +08:00
    @user8341 不可以,假设这个人同时拥有 2,4,还是可能会被选出来。
    user8341
        10
    user8341  
       2020-10-11 19:38:58 +08:00
    你指的是什么呢?下面这个例子里 C 并不会被选出来呀。
    www.db-fiddle.com/f/8rhX2qJheqvz1Gg2t7oasc/0
    fyooo
        11
    fyooo  
    OP
       2020-10-12 09:44:22 +08:00
    @user8341 实测可行,谢谢:)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3293 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:18 · PVG 20:18 · LAX 05:18 · JFK 08:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.