【问题标题】:Counting two tables with several conditions计算有几个条件的两个表
【发布时间】:2021-03-29 14:37:42
【问题描述】:

我有两张桌子,

非诺提波斯

id           DTHHRDX sex
GTEX-1117F   0       2
GTEX-ZE9C    2       1
K-562        1       2

属性

SAMPID                         SMTS
K-562-SM-26GMQ                 Blood vessel
K-562-SM-2AXTU                 Blood_dry
GTEX-1117F-0003-SM-58Q7G       Brain
GTEX-ZE9C-0006-SM-4WKG2        Brain
GTEX-ZE9C-0008-SM-4E3K6        Urine
GTEX-ZE9C-0011-R11a-SM-4WKGG   Urine

我需要知道有多少女性 (sex=2) 有DTHHRDX = 1 并且在SMTS 上有血迹。

例如,这种情况下的答案是 2

【问题讨论】:

    标签: mysql sql count subquery sql-like


    【解决方案1】:

    你可以这样做:

    select count(*) as cnt
    from fenotipos f
    where 
        sex = 2
        and exists (
            select 1
            from atributos a
            where a.sampid like concat(f.id, '%') and a.smts like 'Blood%'
        )
    

    这可以正确处理atributos中潜在的多个匹配项

    或者,您可以加入:

    select count(distinct f.id) as cnt
    from fenotipos f
    inner join atributos a on a.sampid like concat(f.id, '%')
    where f.sex = 2 and a.smts like 'Blood%'
    

    如果没有重复,那么count(*) 在第二个查询中比count(distinct f.id) 更有效。

    【讨论】:

    • 您忘记了 DTHHRDY=1 条件,但我添加了它,非常感谢!!!
    猜你喜欢
    • 2021-03-23
    • 2019-10-28
    • 2019-02-09
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 2013-11-03
    相关资源
    最近更新 更多