mysql选择count大于x的位置

时间:2013-05-18 18:11:37

标签: mysql

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

例如

$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]]]");

希望有所帮助......

3 个答案:

答案 0 :(得分:3)

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

group by baz having count(*) > 5

答案 1 :(得分: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 ;

答案 2 :(得分: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);