0

据我了解,每次查看一行时都会调用子查询,在这种情况下,将查看每一行。

我如何重写这个查询?子查询只需要运行一次,但是当我必须删除组中只有 1 个条目的 id 时,我想不出如何选择 id(我希望组数>1)。

目的是获取与其他行大小相同的行列表

select id 
from file 
where size in 
    ( 
    select size from 
        (
        select count(*) as c, size 
        from file 
        group by size 
        having c>1 and size>0
        ) as t
    )
4

3 回答 3

1

加入反对子查询怎么样?

SELECT  a.*
FROM    `FILE` a 
        INNER JOIN
        (
            SELECT  COUNT(*) as c, `SIZE` 
            FROM    `file `
            WHERE   `size` > 0
            GROUP BY size 
            having  COUNT(*) > 1
        ) b ON a.Size = b.Size
于 2012-11-18T12:36:49.697 回答
1

恕我直言,优化器应该处理它,所以你不必这样做。

让 MySQL 解释语句。

除此之外,您可以将子查询存储在临时表中

更新

CREATE TEMPORARY TABLE tmpTab
  SELECT count(*) AS c, size 
  FROM file 
  GROUP BY size 
  HAVING c>1 AND size>0;

SELECT id 
FROM file 
WHERE size IN 
    ( SELECT size FROM tmpTab )
于 2012-11-18T12:09:53.220 回答
1

您可以只使用一个子查询而不是两个:

select id 
from file 
where size in (
    select size 
    from file 
    group by size 
    having count(*)>1 and size>0
    )

但如果你只想使用连接,你可以使用这样的东西:

select distinct file.id
from file inner join file file1 on file.size = file1.size
where file.size>0 and file.id <> file1.id
于 2012-11-18T12:27:56.470 回答