Mysql 如何 select 仅在相同 table 和相同标识符但值不同的行中

Mysql how to select only row in same table and same identifier but different value

我有这个数据集:

用户计划table:

+----+------+-------+-----------------------+
| ID | userId | name  |      deletedAt      | 
+----+--------+-------+---------------------|
|  1 |   1    | plan1 | 2020-07-30 13:41:50 |
+----+--------+-------+---------------------|
|  2 |   1    | plan3 | NULL                |
+----+--------+-------+---------------------|
|  3 |   2    | plan2 |2020-07-30 15:30:10  |
+----+--------+-------+---------------------|

如何仅 select 这一行(ID 为 3),因为此数据已被删除,但没有任何 deletedAt = NULL 具有相同 userId 的数据?

+----+--------+-------+---------------------|
|  3 |   2    | plan2 |2020-07-30 15:30:10  |
+----+--------+-------+---------------------|

您似乎想要 userid 没有其他行 deletedAtnull 的行。如果是这样,您可以使用 not exists 和相关子查询:

select t.*
from mytable t
where not exists (
    select 1
    from mytable t1
    where t1.userid = t.userid and t1.deletedAt is null
)