【问题标题】:Undefined column: 7 ERROR: column does not exist未定义列:7 错误:列不存在
【发布时间】:2020-01-03 15:11:45
【问题描述】:

我正在使用 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'

它对我有用。所以,问题。如何为 Eloquent ORM 解释我需要“注册”而不是“注册”?

这是我的 Eloquent 代码:

$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" where ("sub"."code" = "registrat ... ^ (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" 内部加入 "customer_events" on " customer_events"."customer_id" = "customers"."id" 内部连接 ​​"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" 有 count(customer_events.id) = 1) 作为 "sub" where ("sub"."code" = "registration" and "sub"."created_at" > "Fri Aug 30 2019 10:03:14 GMT+0000" 和 "sub"."created_at"

【问题讨论】:

  • 您的查询对我来说似乎是错误的。你为什么要这样做:select "sub"."code", "sub"."customer_id", "sub"."data", "sub"."event_count", "sub"."created_at" 而不是:select sub.code, sub.customer_id, sub.data, sub.event_count, sub.created_at?查询中的其他地方也是如此。
  • 我刚刚复制了-&gt;toSql() 方法为我生成的代码。行查询有效。
  • 我明白了。也许“列”有助于它区分 SQL 关键字和表/列。无论如何,您是否尝试过在 phpMyAdmin 或其他工具中手动运行此查询?
  • 我在 DataGrip SQL 客户端中运行原始查询。我知道在任何列名(“column_name”)中添加 " 是不行的。但这只是 Eloquent 的输出。在这个问题上,我不知道如何要求 Eloquent 不要这样做。
  • 这有帮助吗? DB::connection()-&gt;setQueryGrammar(new PostgresGrammar());

标签: php laravel postgresql eloquent


【解决方案1】:

来自docs

whereColumn 方法可用于验证两列是否相等:

您可能应该使用whereInwhere 来验证列的值是否相等,而不是whereColumn 来验证两列是否相等。 (那么应该用单引号正确生成查询)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-06
    • 2020-11-10
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 2018-08-19
    相关资源
    最近更新 更多