0

如何创建一个要求相同查询的总数超过 x 的 MySQL 查询?

例如

$result=mysql_query("select * from DB where BANANAS='1' and ORANGES='2' and [COUNT OF THIS QUERY IS MORE THAN 5]");

谢谢!

编辑

为了更清楚......,我正在寻找的是:

$result=mysql_query("select * from DB where BANANAS='1' and ORANGES='2' group by PINEAPPLES order by SUM(SUGAR) desc limit 1 and [[[COUNT OF PINEAPLES WHERE BANANAS='1' IS MORE THAN 5]]]");

希望有帮助...

4

3 回答 3

3

and (select count(*) from foo where bar) > 5

或者

group by baz having count(*) > 5

于 2013-05-18T18:14:13.753 回答
0

不清楚你要求什么。但是,如果您想要所有行(与条件匹配)但仅当它们是 6 或更多时,这是一种方法:

SELECT * 
FROM tableName         -- have you really named your table "DB"?
WHERE bananas = 1 
  AND oranges = 2
  AND ( SELECT COUNT(*)
        FROM tableName 
        WHERE bananas = 1 
          AND oranges = 2
      ) > 5 ;

另一个:

SELECT * 
FROM tableName         -- have you really named your table "DB"?
WHERE bananas = 1 
  AND oranges = 2
  AND ( SELECT 1
        FROM tableName 
        WHERE bananas = 1 
          AND oranges = 2
        LIMIT 1 OFFSET 5
      ) IS NOT NULL ;
于 2013-05-18T18:23:34.963 回答
0

如果您希望仅在计数大于 5 时进行选择,则以下查询可能有用。

select count(*) from DB as X where BANANAS='1' and ORANGES='2';

select * from DB where BANANAS='1' and ORANGES='2' IF (CAST(X as UNSIGNED) > 5);
于 2013-05-18T18:24:36.860 回答