簡體   English   中英

未定義的列:7錯誤:列不存在

[英]Undefined column: 7 ERROR: column does not exist

我正在使用PostgreSQL。 當我使用“”引號而不是“”時,會發生此錯誤。 很簡單,我會告訴你。 這個SQL查詢給我描述了問題標題中的錯誤

select "sub"."code", "sub"."customer_id", "sub"."data", "sub"."event_count", "sub"."created_at"
from (select "customer_event_types"."code",
             "customer_events"."customer_id",
             "customer_events"."data",
             count(customer_events.id)       event_count,
             max(customer_events.created_at) created_at
      from "customers"
               inner join "customer_events" on "customer_events"."customer_id" = "customers"."id"
               inner join "customer_event_types"
                          on "customer_events"."customer_event_type_id" = "customer_event_types"."id"
      group by "customer_event_types"."code", "customer_events"."customer_id", "customer_events"."data"
      having count(customer_events.id) = 1) as "sub"
where "sub"."code" = "registration"

您在最后一行看到“注冊”了嗎? 它給了我錯誤。

[42703]錯誤:列“注冊”不存在

這是正確代碼的示例:

select "sub"."code", "sub"."customer_id", "sub"."data", "sub"."event_count", "sub"."created_at"
from (select "customer_event_types"."code",
             "customer_events"."customer_id",
             "customer_events"."data",
             count(customer_events.id)       event_count,
             max(customer_events.created_at) created_at
      from "customers"
               inner join "customer_events" on "customer_events"."customer_id" = "customers"."id"
               inner join "customer_event_types"
                          on "customer_events"."customer_event_type_id" = "customer_event_types"."id"
      group by "customer_event_types"."code", "customer_events"."customer_id", "customer_events"."data"
      having count(customer_events.id) = 1) as "sub"
where "sub"."code" = 'registration'

這個對我有用。 所以,這個問題。 如何為雄辯的ORM解釋我需要“注冊”而不是“注冊”?

這是我的口才代碼:

$set = DB::query()->fromSub(function ($query) {
    $query->from('customers')->select('customer_event_types.code',
        'customer_events.customer_id',
        'customer_events.data',
        DB::raw('count(customer_events.id) event_count'),
        DB::raw('max(customer_events.created_at) created_at'))
        ->join('customer_events', 'customer_events.customer_id', '=', 'customers.id')
        ->join('customer_event_types', 'customer_events.customer_event_type_id', 'customer_event_types.id')
        ->groupBy('customer_event_types.code', 'customer_events.customer_id', 'customer_events.data')
        ->having(DB::raw('count(customer_events.id)'), '=', 1);
}, 'sub')
    ->select('sub.code', 'sub.customer_id', 'sub.data', 'sub.event_count', 'sub.created_at')
    ->when($firstItem, function ($query, $events) {
        // making conditions where dynamic 

        $whereColumn = [];
        foreach ($events as $event) {

            $whereColumn[] = ["sub.code", "=", (string)$event->event_code];

            if (isset($event->after_than_minutes_ago) && !empty($event->after_than_minutes_ago) && is_numeric($event->after_than_minutes_ago)) {
                $whereColumn[] = ["sub.created_at", '>', Carbon::now()->subMinutes($event->after_than_minutes_ago)->toString()];
            }
            // произошло раньше, чем <число> минут назад
            if (isset($event->before_than_minutes_ago) && !empty($event->before_than_minutes_ago) && is_numeric($event->before_than_minutes_ago)) {
                $whereColumn[] = ["sub.created_at", '<', Carbon::now()->subMinutes($event->before_than_minutes_ago)->toString()];
            }

            $query->whereColumn($whereColumn);
            $whereColumn = [];
        }
        return $query;
    })
    ->get(); 

如您所見,此代碼比SQL等效代碼更難。 它使where來自輸入數據的條件。 但這似乎並不重要。 我有同樣的錯誤,與引號有關。

SQLSTATE [42703]:未定義的列:7錯誤:列“ registration”不存在第1行:... r_events.id)= $ 1)as“ sub”其中(“ sub”。“ code” =“ registrat ... ^(SQL:從中選擇“ sub”。“ code”,“ sub”。“ customer_id”,“ sub”。“ data”,“ sub”。“ event_count”,“ sub”。“ created_at”從(選擇“ customer_event_types” 。“代碼”,“ customer_events”。“ customer_id”,“ customer_events”。“數據”,count(customer_events.id)event_count,max(customer_events.created_at)created_at來自“ customers”內部聯接“ customer_events”上的“ customer_events”。 “ customer_id” =“客戶”。“ id”內部聯接“ customer_events”上的“ customer_event_types”。“ customer_event_type_id” =“ customer_event_types”。“ id”按“ customer_event_types”分組。“ code”,“ customer_events”。“ customer_id”, count(customer_events.id)= 1)的“ customer_events”。“ data”作為“ sub”,其中(“ sub”。“ code” =“ registration”和“ sub”。” created_at”>“周五8月30日10: 03:14 GMT + 0000“和” sub“。” created_at“ <” 2019年8月30日星期五10:38:14 GMT + 0000“))

文檔

whereColumn方法可用於驗證兩列相等:

您可能應該使用whereInwhere驗證一列的值等於某物,而不是whereColumn驗證兩列是否相等。 (然后,應使用單引號正確生成查詢)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM