thinkphp 子查询算法

//采用thinkphp模式进行的子查询算法

$model = M();
       $subQuery = $model->field('piwik_source')->table('think_user_cart_log')->group('piwik_cookie')->where($where)->order('piwik_cookie')->buildSql(); 
 
       $vlist = $model->field('count(t.piwik_source) as num, t.piwik_source')->table($subQuery.' as t')->group('piwik_source')->order("num desc")->select();
       echo $model->getLastSQL();
       var_dump($vlist);
      // echo $subQuery;
       exit;

//强制手写sql语句的查询方式

 $sql = 'SELECT count(t.piwik_source) as num, t.piwik_source FROM(
            select piwik_source from `think_user_cart_log`  WHERE  piwik_cookie IS NOT NULL OR piwik_cookie !=\'\'   group by  piwik_cookie 
            ) as t group by t.piwik_source
             order by num desc';
 
        $voList = M()->query($sql);
        var_dump($voList);exit;


This entry was posted in JAVA. Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/thinkphp-sub-query/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

*

  

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

About Me

静水流深,水滴石穿