$subQuery1 = D('device_factory df')->join('inner join device d on d.factory=df.factory') ->field("df.name as '机型品牌',d.factory,model,type,case when d.name='' then d.orgin else d.name end as name") ->select(false);
select df.name as '机型品牌',d.factory,model,type,case when d.name='' then d.orgin else d.name end as name from device_factory df inner join device d on d.factory=df.factory
第二个子查询
$subQuery1 = D('device_factory df')->join('inner join device d on d.factory=df.factory') ->field("df.name as '机型品牌',d.factory,model,type,case when d.name='' then d.orgin else d.name end as name") ->select(false); $c = D()->table("record r,($subQuery1)b") ->field('count(*) as times,b.*')->where( 'r.device&0xffff=b.model and r.device>>16&0xffff=b.factory' )->group('device&0xffff')->select(false);
select count(*) as times, b.* from record r ,(select df.name as '机型品牌',d.factory,model,type,case when d.name='' then d.orgin else d.name end as name from device_factory df inner join device d on d.factory=df.factory)b where r.device&0xffff=b.model and r.device>>16&0xffff = b.factory group by device&0xffff
总查询
$subQuery1 = D('device_factory df')->join('inner join device d on d.factory=df.factory') ->field("df.name as '机型品牌',d.factory,model,type,case when d.name='' then d.orgin else d.name end as name") ->select(false); $c = D()->table("record r,($subQuery1)b") ->field('count(*) as times,b.*')->where( 'r.device&0xffff=b.model and r.device>>16&0xffff=b.factory' )->group('device&0xffff')->select(false); $d = D()->table("(select @rank:=0)rank,($c)c") ->field('@rank:=@rank+1 as rank,c.*') ->order('c.times desc')->select();
SELECT @rank:=@rank+1 as rank,c.* FROM (select @rank:=0)rank,( SELECT count(*) as times, b.* FROM record r, (SELECT df.name as '机型品牌',d.factory,`model`,`type`,case when d.name='' then d.orgin else d.name end as name FROM device_factory df inner join device d on d.factory=df.factory )b WHERE ( r.device&0xffff=b.model and r.device>>16&0xffff=b.factory ) GROUP BY device&0xffff )c ORDER BY c.times desc