Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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
Sql 查找带有';彼此不一样';列2匹配时的列1_Sql_Sqlite - Fatal编程技术网

Sql 查找带有';彼此不一样';列2匹配时的列1

Sql 查找带有';彼此不一样';列2匹配时的列1,sql,sqlite,Sql,Sqlite,我在数据库fls2中有一个2列的表,它是Sqlite name | sha256 ------------|------------------ ab/ac/ad | 12345 ab/ad/af | 12345 zx/ad/af | 12345 我想找到“name like'ab%”和“name not like'ab%”对于特定的sha256都是正确的名称。因此,在上述情况下,3行共享“12345”的Sa256,并且我想考虑子数据集。

我在数据库fls2中有一个2列的表,它是Sqlite

name        |    sha256
------------|------------------
ab/ac/ad    |    12345
ab/ad/af    |    12345
zx/ad/af    |    12345
我想找到“name like'ab%”和“name not like'ab%”对于特定的sha256都是正确的名称。因此,在上述情况下,3行共享“12345”的Sa256,并且我想考虑子数据集。在该数据集中,如果'name like'ab%'和'name not like'ab%'都为真(显然对于2行或更多不同的行),我希望返回所有行

我所做的是搜索两个不同的顶级目录中存在相同文件(由其sha256标识)的情况

在获取数据后,我知道如何在perl中执行此操作,但理想情况下,如果我可以在DB中执行此操作,则效果会更好。我试过了

select name 
from 
    fls2 
where 
    sha256 = (select sha256 from fls2 where name like 'ab%') 
and 
    name not like 'ab%';
但它不会返回任何行(我知道至少有一些行,因为我已经手动找到了它们)

在该数据集中,如果'name like'ab%'和'name not like'ab%'都为真(显然对于2行或更多不同的行),我希望返回所有行

您可以使用窗口功能:

select name, sha256
from (
    select 
        f.*,
        max(case when name like 'ab%' then 1 end) over(partition by sha256) max_ab,
        max(case when name not like 'ab%' then 1 end) over(partition by sha256) max_not_ab
    from fls2 f
) t
where max_ab = 1 and max_not_ab = 1
在子查询中,窗口
max()。然后,外部查询过滤满足这两个条件的记录

在该数据集中,如果'name like'ab%'和'name not like'ab%'都为真(显然对于2行或更多不同的行),我希望返回所有行

您可以使用窗口功能:

select name, sha256
from (
    select 
        f.*,
        max(case when name like 'ab%' then 1 end) over(partition by sha256) max_ab,
        max(case when name not like 'ab%' then 1 end) over(partition by sha256) max_not_ab
    from fls2 f
) t
where max_ab = 1 and max_not_ab = 1

在子查询中,窗口
max()。然后,外部查询对满足这两个条件的记录进行筛选。

使用聚合和
having

select sha226, group_concat(name) as names
from t
group by sha226
having sum(case when name like 'ab%' then 1 else 0 end) > 0 and
       sum(case when name not like 'ab%' then 1 else 0 end) > 0;

这会将列表中的所有名称放在同一行。

使用聚合和
拥有:

select sha226, group_concat(name) as names
from t
group by sha226
having sum(case when name like 'ab%' then 1 else 0 end) > 0 and
       sum(case when name not like 'ab%' then 1 else 0 end) > 0;

这会将列表中的所有名称放在同一行。

使用
存在

select * from fls2 f
where
  exists (select 1 from fls2 where sha256 = f.sha256 and name like 'ab%')
  and
  exists (select 1 from fls2 where sha256 = f.sha256 and name not like 'ab%')
请参阅。
或使用
sum()
窗口函数:

select f.name, f.sha256
from (
  select *, 
    sum(name like 'ab%') over (partition by sha256) sum1,
    sum(name not like 'ab%') over (partition by sha256) sum2
  from fls2
) f
where f.sum1 > 0 and f.sum2 > 0
请参阅。
结果:


使用
存在

select * from fls2 f
where
  exists (select 1 from fls2 where sha256 = f.sha256 and name like 'ab%')
  and
  exists (select 1 from fls2 where sha256 = f.sha256 and name not like 'ab%')
请参阅。
或使用
sum()
窗口函数:

select f.name, f.sha256
from (
  select *, 
    sum(name like 'ab%') over (partition by sha256) sum1,
    sum(name not like 'ab%') over (partition by sha256) sum2
  from fls2
) f
where f.sum1 > 0 and f.sum2 > 0
请参阅。
结果:


我做错了什么吗?我得到了“Error:near”from:“syntax Error”,我不太理解这个查询,不知道为什么。@Pete:from的
前面有一个不需要的逗号。。。修复。很难选择接受3个工作答案中的哪一个,但我最终使用了这一个,所以选择了这一个thx。我是否做错了什么-我得到了“错误:接近”from:“语法错误”,我不太理解这个查询,不知道为什么。@Pete:from的
之前有一个不需要的尾随逗号。
。。。修正。很难选择接受3个工作答案中的哪一个,但我最终使用了这一个,所以我选择了这一个。@Pete,是吗?我看不出来。你说得对。对不起,修好了。刚刚发布了一个类似的问题,我很乐意将它奖励给第一个能告诉我怎么做的人btw@Pete是吗?我看不出来。你说得对。对不起,修好了。刚刚发布了一个类似的问题,我很高兴奖励给第一个能告诉我怎么做的人