Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.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_Subquery_Aggregate Functions_Where Clause - Fatal编程技术网

Mysql SQL选择一个值,但同时考虑其他值

Mysql SQL选择一个值,但同时考虑其他值,mysql,sql,subquery,aggregate-functions,where-clause,Mysql,Sql,Subquery,Aggregate Functions,Where Clause,这就是桌子的样子 Number UniqID status 555 1 in 555 1 in 555 1 in 555 2 in 555 2 out 555 2 in 我想选择这样的 Number UniqID status 555 1 in 555 1 in 555 1

这就是桌子的样子

Number   UniqID   status
555      1        in
555      1        in
555      1        in
555      2        in
555      2        out
555      2        in
我想选择这样的

Number   UniqID   status
555      1        in
555      1        in
555      1        in
并且仅当所有的相同的uniqid都具有中的状态时才选择它。如果其中一个状态为同一个ID显示out,则跳过整个事件。此外,UniqID也会自动生成

我想展示一下

Number    status
555       in

您可以在
不存在的情况下获得第一个结果集

select *
from mytable t
where 
    t.status = 'in' 
    and not exists (
        select 1 
        from mytable t1 
        where t1.number = t.number and t1.uniqid = t.uniqid and t1.status = 'out'
    )
另一方面,如果希望所有状态均为“in”的
(number,uniqid)
元组,则聚合更简单:

select number, uniqid, min(status) as status
from mytable
group by number, uniqid
having min(status) = max(status) and min(status) = 'in'

UniqID不是很独特,是吗?但您需要一个“notexists”子选项,如下所示:您好,谢谢您的快速回复。但是如果唯一ID未知,它是随机生成的,我仍然想获取所有uniqID状态为“in”的“number”,如果其中一个状态为“out”,则跳过整个过程thing@BornToLive:以上查询没有硬编码任何关于
unqid
,因此它们应该执行您想要的操作(它们将返回您为示例数据显示的结果)。请注意,称为
uniqid
的内容在表中实际上不是唯一的(如示例数据中所示),这与直觉是相反的我还有一个问题,最后的结果是怎样得到一个555而不是同一个条目中的3个不用担心,我已经弄明白了,再次感谢你的帮助