应网友帮忙解决查询发布信息按地区统计问题
原模版写法:
<?php $newbook=$empire->query("select city,count(*) as num from {$dbtbpre}ecms_weixin where city<>'' group by city order by num desc limit 60"); while($r=$empire->fetch($newbook)) { $c=$empire->fetch1("select id,name from {$dbtbpre}extend_linkage where id='$r[city]' limit 1"); ?> <li><a><?=str_replace('市','',esub($c['name'],10))?>(<span class="c44b"><?=$r['num']?></span>)</a></li> <?}?>
一看栏目对应模型才知道需要统计的地区信息都跨表了,于是想想能否提供UNION ALL多次select查询统计,测试后的查询代码写法
<?php $newbook=$empire->query("select a.city,count(*) num from (select id,city from {$dbtbpre}ecms_weixin where city<>'' UNION ALL select id,city from {$dbtbpre}ecms_openid where city<>'' UNION ALL select id,city from {$dbtbpre}ecms_group where city<>'') a group by a.city order by num desc limit 60"); while($r=$empire->fetch($newbook)) { $c=$empire->fetch1("select id,name from {$dbtbpre}extend_linkage where id='$r[city]' limit 1"); ?> <li><a><?=str_replace('市','',esub($c['name'],10))?>(<span class="c44b"><?=$r['num']?></span>)</a></li> <?}?>
最终查询展示效果
如果本文对你有帮助,欢迎打赏本站