Mysql SQL选择其他位置列的行为空的行

Mysql SQL选择其他位置列的行为空的行,mysql,sql,Mysql,Sql,首先,如果标题令人困惑,请道歉。 SQL不是我的强项,我在这方面已经工作了一段时间,我在mmoment的想法是加入,也许还有团队 Soto示例: record | type | key1 | key2 | data1 --------------------------------------- 1 | 1 | joe | smoe | 10 2 | 2 | homer | simpson | 20 3 | 1 | null |

首先,如果标题令人困惑,请道歉。
SQL不是我的强项,我在这方面已经工作了一段时间,我在mmoment的想法是加入,也许还有团队

Soto示例:

record | type | key1  | key2    | data1
---------------------------------------
1      | 1    | joe   | smoe    | 10
2      | 2    | homer | simpson | 20
3      | 1    | null  | null    | 30
4      | 3    | bart  | simpson | 40 
其中主键由id、键1、键2组成

我只想要“type”行,其中key1不为null,key2不为null

因此,由于在记录3中,类型1有空键,因此我希望类型1的所有记录都不包括在派生表中。

这里有一种相关的“不存在”方法:

select *
from T as t1
where not exists (
    select *
    from T as t2
    where t2.type = t1.type and (t2.key1 is null or t2.key2 is null)
)
这里有一个使用非相关查询和分组的方法。也许这就是你的想法:

select *
from T as t1
where t1.type in (
    select t2.type
    from T as t2
    group by t2.type
    having count(*) = count(t2.key1) and count(*) = count(t2.key2)
)
因为我知道mysql查询计划可能对这些事情很敏感。下面是连接的等效项:

select t1.*
from T as t1
    inner join
    (
        select t2.type
        from T as t2
        group by t2.type
        having count(*) = count(t2.key1) and count(*) = count(t2.key2)
    ) as goodtypes
        on goodtypes.type = t1.type
以下是一种相关的“不存在”方法:

select *
from T as t1
where not exists (
    select *
    from T as t2
    where t2.type = t1.type and (t2.key1 is null or t2.key2 is null)
)
这里有一个使用非相关查询和分组的方法。也许这就是你的想法:

select *
from T as t1
where t1.type in (
    select t2.type
    from T as t2
    group by t2.type
    having count(*) = count(t2.key1) and count(*) = count(t2.key2)
)
因为我知道mysql查询计划可能对这些事情很敏感。下面是连接的等效项:

select t1.*
from T as t1
    inner join
    (
        select t2.type
        from T as t2
        group by t2.type
        having count(*) = count(t2.key1) and count(*) = count(t2.key2)
    ) as goodtypes
        on goodtypes.type = t1.type

不需要漂亮的短语,也不需要缩写。我认为你的标题很好。不需要漂亮的词组,也不需要缩写。我认为你的头衔很好。