我有这个查询:
$stmt = $dbh->prepare("SELECT DISTINCT country_code,country_name FROM users WHERE country_code!='NA' AND country_name!='NA'");
$stmt->execute();
$rows = $stmt->fetchAll();
它清楚地从我的数据库中选择了一些信息。
然后我在foreach循环中循环数据:
foreach ($rows as $row){
//Do something
}
尽管如此,我希望只能使用结果/数据,如果原始查询中有5个以上的结果。
示例:
如果具有值CA
和canada
的country_code
和country_name
存在5次以上。
您可以使用group by,并且必须获得特定的结果。您可以使用count()函数按特定列对行组的数量进行计数。如果您只希望country_code='CA',也可以添加表达式,如下例所示。
select country_code, country_name from user
group by country_code
having country_code='CA' and count(country_code) >=5
这是一个例子http://sqlfiddle.com/#!9/b62530/1