首頁  >  文章  >  php框架  >  解決laravel用clickhouse查詢出現「Missing columns」問題

解決laravel用clickhouse查詢出現「Missing columns」問題

藏色散人
藏色散人轉載
2022-10-31 16:00:313016瀏覽

以下由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;

以上是解決laravel用clickhouse查詢出現「Missing columns」問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:learnku.com。如有侵權,請聯絡admin@php.cn刪除