Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.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-如何选择列中有值1但在同一列中没有值2的所有行?_Mysql_Sql - Fatal编程技术网

Mysql SQL-如何选择列中有值1但在同一列中没有值2的所有行?

Mysql SQL-如何选择列中有值1但在同一列中没有值2的所有行?,mysql,sql,Mysql,Sql,我有这样一个sql表: ------------------------------------------ ID | SKU | Name | Type ------------------------------------------- 2 | ABC | Pasta | 2 3 | XYZ | Maggi | 5 2 | ABC | Pasta | 5 6

我有这样一个sql表:

------------------------------------------
ID    | SKU      | Name        |  Type
-------------------------------------------
2     | ABC      | Pasta       |  2
3     | XYZ      | Maggi       |  5
2     | ABC      | Pasta       |  5
6     | MNO      | Macroni     |  2
3     | XYZ      | Maggi       |  0
3     | XYZ      | Maggi       |  2
我需要查找具有类型2但没有类型5或0项的行

例如: 这应该会导致

------------------------------------------
ID    | SKU      | Name        |  Type
-------------------------------------------
6     | MNO      | Macroni     |  2
仅使用这一张表是否可能产生这样的结果?当然是。 此表有1.4M行,我使用了此查询(不确定是否正确)

查询从未返回任何内容,因为它仍在运行。
您能提供帮助吗?

使用
分组方式尝试此操作,使用条件计数查找包含2而不是0或5的ID,然后使用
中的
获取相关行

select *
from your_table
where id in (
    select
        id
    from your_table t
    group by id
    having count(case when type = 2 then 1 end) > 0
    and count(case when type in (0,5) then 1 end) = 0
);

我喜欢通过
拥有
分组方式

select e.id
from reportmal e
group by e.id
having sum(e.type = 2) > 0 and
       sum(e.type in (0, 5)) = 0;
MySQL将布尔表达式视为数值上下文中的整数,其中“1”表示true,“0”表示false。因此
sum(e.type=2)
统计
type=2的每个组中的记录数。
>0
表示至少存在一个这样的行。
=0
表示不存在此类行

如果需要原始行,可以
将该结果连接回表。

听起来像是典型的“不存在”-查询:

或:

至于性能问题-
您至少需要一个reportmal(id)索引。

reportmal(id,type)上的索引可能更好。

我认为您应该使用以下内容:

SELECT a.*
  FROM reportmal a
 WHERE a.type = 2
   AND NOT EXISTS (SELECT 1
                     FROM reportmal b
                    WHERE b.name = a.name
                      AND b.type IN (5,0));
就像: 类型为2的所有行,但不存在名称相同且类型为5或0的行


我不知道定义“条目”的参数是什么。也许您应该在子查询中添加SKU(这完全取决于您的数据模型)。

您的查询很接近,但您是在Id上加入的,该Id将为您提供笛卡尔乘积

我就是这样做的

Select p1.*
    From pasta p1
        Left outer join pasta p2 on p1.sku=p2.sku
            And p2.type in (0,5)
        Where p2.type is null

希望这对你有帮助

这是一个极好的方法。你能解释一下这里的求和函数吗?这会导致什么结果?对不起,如果我太天真了,你用它来代替计数吗?我还没有检查这个查询,但它确实比嵌套查询有很大的好处selects@th3pirat3-这不是必需的结果应该是
sku
而不是
id
,并且需要在输出中使用其他字段。@th3pirat3
SUM()
,因为表达式返回1或0,如果使用
COUNT()
,两者都将被计数,因为它们是非空值。如果在无
ELSE
条件下使用
CASE
表达式,则
COUNT()
将起作用。
select * from atable result
where result.type = 2 and result.sku not in
 (select sku from atable no50
  where no50.type in (5,0)
 )
select  *
from    reportmal e
where   e.type = 2
    and not exists 
        (
            select  null
            from    reportmal e2
            where   e2.id = e.id
                and e2.type in (0,5)
        ) 
SELECT a.*
  FROM reportmal a
 WHERE a.type = 2
   AND NOT EXISTS (SELECT 1
                     FROM reportmal b
                    WHERE b.name = a.name
                      AND b.type IN (5,0));
Select p1.*
    From pasta p1
        Left outer join pasta p2 on p1.sku=p2.sku
            And p2.type in (0,5)
        Where p2.type is null