Rumah  >  Artikel  >  rangka kerja php  >  解决laravel用clickhouse查询出现“Missing columns”问题

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

藏色散人
藏色散人ke hadapan
2022-10-31 16:00:313037semak imbas

下面由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(&#39;create_time&#39;, &#39;<=&#39;, $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(&#39;clickhouse&#39;)
        ->table((new AccessLogs)->getTable());
    if(!empty($cookieId)) {
        $builder->where(&#39;cookie_id&#39;, $cookieId);
    }        
    if(!empty($host)) {
        $builder->where(&#39;host&#39;, $host);
    }        
    if(!empty($uri)) {
        $builder->where(&#39;uri&#39;, $uri);
    }
    if(!empty($startTime)) {
        $builder->where(&#39;create_time&#39;, &#39;>=&#39;, $startTime);
    }
    if(!empty($endTime)) {
        $builder->where(&#39;create_time&#39;, &#39;<=&#39;, $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;

Atas ialah kandungan terperinci 解决laravel用clickhouse查询出现“Missing columns”问题. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:learnku.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam