php教程解决laravel用clickhouse查询出现“Missing columns”问题

 所属分类:php教程

 浏览:236次-  评论: 0次-  更新时间:2022-12-05
描述:更多教程资料进入php教程获得。 下面由Laravel教程栏目给大家介绍关于在laravel中使用clickhouse查询引起的“DB::Exception: Missing col...
更多教程资料进入php教程获得。 下面由Laravel教程栏目给大家介绍关于在laravel中使用clickhouse查询引起的“DB::Exception: Missing columns”问题,希望对大家有所帮助!

使用 clickhouse 尤其注意:不能这么写!

   $where = [];
    if($cookieId) {
        $where['cookie_id'] = $cookieId;
    }        
    if($host) {
        $where['host'] = $host;
    }        
    if($uri) {
        $where['uri'] = $uri;
    }
    $builder = DB::connection('clickhouse')
        ->table((new AccessLogs)->getTable())
        ->where($where);
    if(!empty($startTime)) {
        $builder->where('create_time', '>=', $startTime);
    }
    if(!empty($endTime)) {
        $builder->where('create_time', '<=', $endTime);
    }
登录后复制

当多个条件查询时,$where 数组在 sql 中会被当成一个字段,从而导致 DB:: Exception: Missing columns: ‘2022-09-27 13:00:49’ ‘2022-09-27 16:00:49’ while processing query 的错误。

这样优化:

   $builder = DB::connection('clickhouse')
        ->table((new AccessLogs)->getTable());
    if(!empty($cookieId)) {
        $builder->where('cookie_id', $cookieId);
    }        
    if(!empty($host)) {
        $builder->where('host', $host);
    }        
    if(!empty($uri)) {
        $builder->where('uri', $uri);
    }
    if(!empty($startTime)) {
        $builder->where('create_time', '>=', $startTime);
    }
    if(!empty($endTime)) {
        $builder->where('create_time', '<=', $endTime);
    }
登录后复制

才能正确查询。

多提一句:在命令行查询时,参数值使用单引号,使用双引号时,参数值也会被当成一个字段:

正确操作是:

select * from access_log where create_time >= ‘2022-09-27 13:00:49’ and create_time <= ‘2022-09-27 16:00:49’ order by create_time desc limit 10;
登录后复制

以上就是解决laravel用clickhouse查询出现“Missing columns”问题的详细内容,更多请关注zzsucai.com其它相关文章!

 标签: Laravel,
积分说明:注册即送10金币,每日签到可获得更多金币,成为VIP会员可免金币下载! 充值积分充值会员更多说明»

讨论这个素材(0)回答他人问题或分享使用心得奖励金币

〒_〒 居然一个评论都没有……

表情  文明上网,理性发言!