Home >Database >Mysql Tutorial >mysql筛选GROUP BY多个字段组合时的用法分享_MySQL

mysql筛选GROUP BY多个字段组合时的用法分享_MySQL

WBOY
WBOYOriginal
2016-06-01 13:20:521057browse

bitsCN.com

想实现这样一种效果

如果使用group by一个条件的话,得到的结果会少了很多,如何多个条件组合筛选呢

group by fielda,fieldb,fieldc...

循环的时候可以通过判断后一个跟前面一个是否相同来分组,一个示例

$result = mysql_query("SELECT groups,name,goods FROM table GROUP BY groups,name ORDER BY name");
$arr = array();
$i = 0;
while($row = mysql_fetch_array($result)){
$arr[] = $row;
}

$m = $i - 1;
foreach($arr as $k=>$v){
if($v[$i]['groups'] $v[$m]['groups']){
echo '分割符号';
}
echo $v['name'],$v['goods'];
}

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn