• ThinkPHP连贯查询之子查询


    $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
  • 相关阅读:
    Navicat for MySQL远程连接的时候报错mysql 1130的解决方法
    阿里云主机 CentOS6.5 安装Mysql php Apache
    MAC下使用feddler进行抓包
    javascript钩子之Backbone里的实现
    SASS编译
    动态代理模式和AOP探究
    二分查找算法
    MyBatis在非Spring环境下第三方DataSource设置-Druid篇
    写字节流转换String 代码示例
    SpringAOP代理报错问题
  • 原文地址:https://www.cnblogs.com/cexm/p/6293176.html
Copyright © 2020-2023  润新知