union统计
$sql = "select count(*) as total from((select t.id from arctype t,article a where t.iszt>0 and t.keyarr!='' and a.id=t.keyarr and t.iscap=1 $addCondition)union(select t.id from article a,arctype t where a.typeid=t.id and t.iszt=3 and t.iscap=1 $yxscCondition))
as a";
注意:刚开始()后没加 as a,执行后报错,提示需要添加别名,union结果作为表查询时必须加别名as table
union排序分页
$sql = "(select a.litpic as icon,a.id,a.pubdate,a.shorttitle as title,t.keyarr,t.domainname,a.typeid,t.iszt from article a,arctype t where t.iszt>0 and t.keyarr!='' and a.id=t.keyarr and t.iscap=1 $addCondition) union (select a.icon,a.id,a.pubdate,a.title,t.keyarr,t.domainname,a.typeid,t.iszt
from article a,arctype t where a.typeid=t.id and t.iszt=3 and t.iscap=1 $yxscCondition) order by pubdate desc limit $start,$pageSize";
注:像表一样操作就可以了
本文介绍如何使用SQL的Union操作符进行记录合并,并展示了如何实现查询结果的排序及分页显示。通过具体示例代码,读者可以了解到正确添加别名的重要性以及如何像操作普通表一样对Union结果进行排序和分页。
919

被折叠的 条评论
为什么被折叠?



