我的表如下:

 guid version

 A1   lol
 A2   roflmao
 A1   lol
 A1   lol
 A2   lol
 A3   roflmao
 A3   roflmao
 A3   roflmao
 A3   roflmao
 A3   roflmao
 A3   roflmao
 A4   roflmao
 A4   roflmao
 A5   lol
 A5   lol


我想计算一下,那么roflmao出现的次数总共少于3次(每个GUID)。

到目前为止,我有以下内容,但未输出任何内容:

$sql="select guid,version, count(*) as `count`
from mytable WHERE version = 'roflmao' group by guid having `count` < 3'";

if ($result=mysqli_query($con,$sql))
{

 // Return the number of rows in result set

 $rowcount=mysqli_num_rows($result);
 printf($rowcount);

 // Free result set

 mysqli_free_result($result);
 }

最佳答案

尝试这个:

SELECT COUNT(*) FROM (
    SELECT guid
    FROM YourTable
    WHERE version = 'roflmao'
    GROUP BY guid
    HAVING COUNT(*) <3) t


首先,选择所有出现“ roflmao”的向导,使其少出现3次,然后对它们进行计数。

09-20 18:04