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 · Oct 11, 2020 · 4558 views
    This topic created in 2066 days ago, the information mentioned may be changed or developed.

    老菜鸟对 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)

    Supplement 1  ·  Oct 11, 2020
    谢谢几位的回复,已经发送感谢:)
    11 replies    2020-10-12 09:44:22 +08:00
    wushigejiajia01
        1
    wushigejiajia01  
       Oct 11, 2020 via Android   ❤️ 1
    我也疑惑了

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

    啊,不是这样吗?
    qinxi
        2
    qinxi  
       Oct 11, 2020   ❤️ 1
    where toys in (2,3) and hp > 5 and agi >5
    wangyanrui
        3
    wangyanrui  
       Oct 11, 2020 via Android
    两个 in 的查询
    wangyanrui
        4
    wangyanrui  
       Oct 11, 2020 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
       Oct 11, 2020
    @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
       Oct 11, 2020
    @wangyanrui 谢谢哈,实测可行:)
    wushigejiajia01
        7
    wushigejiajia01  
       Oct 11, 2020
    @fyooo
    嗯嗯, 我的锅, 审题不全
    9LCRwvU14033RHJo
        8
    9LCRwvU14033RHJo  
       Oct 11, 2020   ❤️ 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  
       Oct 11, 2020
    @user8341 不可以,假设这个人同时拥有 2,4,还是可能会被选出来。
    9LCRwvU14033RHJo
        10
    9LCRwvU14033RHJo  
       Oct 11, 2020
    你指的是什么呢?下面这个例子里 C 并不会被选出来呀。
    www.db-fiddle.com/f/8rhX2qJheqvz1Gg2t7oasc/0
    fyooo
        11
    fyooo  
    OP
       Oct 12, 2020
    @user8341 实测可行,谢谢:)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2931 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 14:59 · PVG 22:59 · LAX 07:59 · JFK 10:59
    ♥ Do have faith in what you're doing.