Home > PHP Framework > Laravel > body text

Solve the 'Missing columns' problem when laravel uses clickhouse query

藏色散人
Release: 2022-10-31 16:00:31
forward
3195 people have browsed it

The following column Laravel Tutorial will introduce to you the "DB::Exception: Missing columns" problem caused by using clickhouse query in laravel. I hope it will be helpful to everyone!

Use clickhouse Special attention: you cannot write like this!

   $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);
    }
Copy after login

When querying with multiple conditions, the $where array will be treated as a field in SQL, resulting in DB::Exception: Missing columns: '2022-09-27 13:00:49' '2022 -09-27 16:00:49' error while processing query.

Optimize like this:

   $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);
    }
Copy after login

can query correctly.

One more thing to mention: when querying on the command line, use single quotes for parameter values. When using double quotes, the parameter value will also be treated as a field:

The correct operation is:

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;
Copy after login

The above is the detailed content of Solve the 'Missing columns' problem when laravel uses clickhouse query. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template