Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL:仅当同一表中没有其他具有特殊不同值的条目时才显示条目_Mysql_Sql_Select_Count_Distinct - Fatal编程技术网

Mysql SQL:仅当同一表中没有其他具有特殊不同值的条目时才显示条目

Mysql SQL:仅当同一表中没有其他具有特殊不同值的条目时才显示条目,mysql,sql,select,count,distinct,Mysql,Sql,Select,Count,Distinct,这是我的示例表。因此,我希望它只显示状态为1、按ID_A分组的条目。它不能包含状态为2的结果! 结果应该如下所示: ID ID_A Status 175 473 2 174 473 1 173 455 2 170 412 2 169 397 1 168 393 2 173 391 2 我的问题是可能有两个相似的ID_A条目。不知道用计数还是区分是容易做到的?我现在不知怎么搞不懂。。提前谢谢 这有

这是我的示例表。因此,我希望它只显示状态为1、按ID_A分组的条目。它不能包含状态为2的结果! 结果应该如下所示:

ID    ID_A   Status  
175   473    2    
174   473    1    
173   455    2
170   412    2
169   397    1
168   393    2
173   391    2

我的问题是可能有两个相似的ID_A条目。不知道用计数还是区分是容易做到的?我现在不知怎么搞不懂。。提前谢谢

这有时称为排除连接

执行外部联接以尝试查找将使条件无效的行。如果没有这样的行,外部联接将把NULL放入联接表的所有列中,然后您就有了一个匹配项

ID_A   Status
397    1

重新评论@草莓:

2001年的美国专利定义了排除连接:

排除联接操作选择第一个表中具有指定列中的值的行,而这些值在第二个表的指定列中找不到。数据库系统可以通过排除第一个表中与第二个表中的条目相匹配的条目来执行这样的查询

该专利还引用了1993年的一篇论文


我假设这个术语也早于那篇论文。

您可以通过聚合和
拥有
子句来实现这一点:

SELECT t1.ID_A, t1.Status
FROM exampletable AS t1
LEFT OUTER JOIN exampletable AS t2
    ON t1.ID_A = t2.ID_A AND t2.Status = 2 
WHERE t1.Status = 1
    AND t2.ID IS NULL;
试一试


您可以这样做:首先从列中查找唯一的条目,然后根据您的条件查找确切的值

select distinct x.id_a, x.status
  from tbl x
 where status = 1
   and not exists (select 'y'
          from tbl y
         where x.id_a = y.id_a
           and status = 2)

希望能有所帮助。

从x中选择不同的x.id\u x.status,在y.id\u a=x.id\u a和y.status=2上加入y,其中x.status=1,y.id\u a为空;我也把它称为排除加入,但我不知道我是否曾在任何地方看到过这种情况。虽然这个链接可能会回答这个问题,但最好在这里包含答案的基本部分,并提供链接供参考。如果链接页面更改,只有链接的答案可能会无效。我没有在这篇文章中添加任何链接,你是想评论其他内容吗?
select t.id_A,t.status
from exampletabla as t
where t.status = 1 and t.id_A not in (select id_A from exampletabla where status = 2)
select distinct x.id_a, x.status
  from tbl x
 where status = 1
   and not exists (select 'y'
          from tbl y
         where x.id_a = y.id_a
           and status = 2)
select ID_A ,Status 
FROM testTbl 
group by ID_A 
having COUNT(*) = 1 and Status  = 1;