Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.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在where子句中使用count(1)?_Mysql_Sql_Count - Fatal编程技术网

MYSQL在where子句中使用count(1)?

MYSQL在where子句中使用count(1)?,mysql,sql,count,Mysql,Sql,Count,我在做面部识别。我有一个a组和B组的人的数据库。我想检查a组的每个人和B组的每个人。我有很多不同的算法来验证这些脸。为此,我设置了以下表格 comparison ( id int, personA_id int, personB_id int, ) facerecScore ( id int, score int, comparison_id int, algo_id int, ) 假设我有一个eigenfaces程序作为我正在测试的

我在做面部识别。我有一个a组和B组的人的数据库。我想检查a组的每个人和B组的每个人。我有很多不同的算法来验证这些脸。为此,我设置了以下表格

comparison (
    id int,
    personA_id int,
    personB_id int,
)

facerecScore (
    id int,
    score int,
    comparison_id int,
    algo_id int,
 )
假设我有一个eigenfaces程序作为我正在测试的第一个算法运行。特征脸的
算法id
为1

我要做的是做一个查询,从比较中选择
personA
personB
,其中
facerecScore
表中不存在任何记录,其中
algou id
为1,比较就是该比较


换句话说,如果我已经在这两个人身上运行过eigenfaces,我就不想再运行它了。因此,我不想选择一个在
facerecscore
表中已经有记录且
algou id
为1的比较,您可以尝试以下方法,它将找到
comparison
中对于给定的
algou id
facerecscore
中没有记录的所有行参数
:当前算法

SELECT *
FROM comparison
WHERE id not in (
    SELECT comparison_id
    FROM facerecScore
    WHERE algo_id = :current_algo
);
在您希望为所有
algou id
中没有相应记录的
facerecScore
查找所有比较行的场景中,您可以使用如下内容

SELECT *
FROM comparison, (SELECT algo_id FROM facerecScore GROUP BY algo_id) algo    
WHERE id not in (
    SELECT comparison_id
    FROM facerecScore
    WHERE algo_id = algo.algo_id
);

只需此查询首先查找
比较
行和
算法id
的所有组合,然后从结果集中删除任何在
facerecScore
中有记录的组合。

您可以使用此查询,它将返回第一个未触及的组合。删除最后一部分
Limit 1,1
,您将获得所有未触及的组合

SELECT *
  FROM comparison
 WHERE id
not in (
       select comparison_id
       from facerecScore
       where algo_id = 1)
 Limit 1,1

这对于子查询的效率来说可能非常糟糕,但它应该会给出正确的结果。也许其他人可以找到更有效的解决方案

对于不喜欢相关子查询的任何人(例如,出于性能原因,如果原始查询未优化),可以使用左连接并排除实际连接的任何行:

更新:灵感来自@penfold的“全部查找”答案,如果
algo\u id
s的列表已知(且简短),则这是一个加入+联合的备选方案:

或者更一般的一个(不确定哪一个性能更好):


是的,我只是在修补自己身上没有的东西。我回来检查,发现其他人都有同样的想法。谢谢大家!@纳特尔用一种更广义的方法更新了我的答案,它不需要参数。@ NATEL“其他人”在8分钟后很难猜到+ 1,对于“查找全部”版本,仍然<代码>不在< /代码>如果不存在任何索引,那么如果性能不好,请考虑我的答案。
SELECT personA_id, personB_id FROM comparison WHERE id NOT IN (SELECT comparison_id FROM facerecScore WHERE algo_id = 1);
select '1' algo_id, c.*
  from comparison c
  left join facerecScore f
    on c.id = f.comparison_id
    and f.algo_id = 1
  where f.id is null
union all
select '2' algo_id, c.*
  from comparison c
  left join facerecScore f
    on c.id = f.comparison_id
    and f.algo_id = 2
  where f.id is null
...
select a.algo_id, c.id
  from comparison c
  cross join (select algo_id from facerecScore group by algo_id) a
  left join facerecScore f
    on c.id = f.comparison_id
    and f.algo_id = a.algo_id
  where f.id is null