【问题标题】:SQLSTATE[42S22]: Column not found: 1054 Unknown column (Seems to be trying to use ID as column name)SQLSTATE[42S22]: Column not found: 1054 Unknown column (似乎试图使用 ID 作为列名)
【发布时间】:2018-02-04 13:07:14
【问题描述】:

所以我最近一直在与 Laravel 中的一个范围作斗争,我收到以下错误:

SQLSTATE[42S22]:未找到列:1054 未知列 'on 子句'中的'visitors.visitorable_id'(SQL:选择profiles.*, profile_genres.genre_id 作为pivot_genre_id, profile_genres.profile_id 作为pivot_profile_id 来自profiles 内部连接profile_genresprofiles.id = profile_genres.profile_id left join (SELECT COUNT(id) 访问, ip_address、visitorable_id、visitorable_type FROM visitors GROUP BY ip_address) 出现在visitors.visitorable_id = db885a18-f0b7-4f55-9d93-743fbb5d9c94visitors.visitorable_type = App\Profile 在哪里 profile_genres.genre_id = 038ba398-8998-4cd6-950c-60b4a6c401cc 和 profiles.deleted_at 为空)

这是导致此错误的查询:

public function scopePopular($query, $limit = 10)
    {
        $query->leftJoin(DB::raw('(SELECT COUNT(id) visits, ip_address, visitorable_id, visitorable_type FROM `visitors` GROUP BY ip_address) occurences'), function($join)
        {
            $join->on('visitors.visitorable_id', '=', 'db885a18-f0b7-4f55-9d93-743fbb5d9c94');
            $join->where('visitors.visitorable_type', '=', 'App\Profile');
        });
        return $query;
    }

编辑 根据要求,我的访问者表迁移:

public function up()
    {
        Schema::create('visitors', function(Blueprint $table) {
            $table->increments('id');

            $table->uuid('visitorable_id');
            $table->string('visitorable_type');

            $table->string('isp')->nullable();
            $table->string('country_code')->nullable();
            $table->string('country')->nullable();
            $table->string('city')->nullable();
            $table->string('region')->nullable();
            $table->string('region_name')->nullable();
            $table->string('ip_address')->nullable();
            $table->string('timezone')->nullable();
            $table->string('zip_code')->nullable();
            $table->string('latitude')->nullable();
            $table->string('longitude')->nullable();

            $table->timestamps();
        });
    }

【问题讨论】:

  • 未知列“visitors.visitorable_id”。桌子上是否存在visitorable_id?
  • 您能发布您的visitors 表的架构吗?我认为您指的是错误的列
  • 用我的迁移更新了问题

标签: php mysql laravel eloquent query-builder


【解决方案1】:
SELECT
    profiles.*, 
    profile_genres.genre_id as pivot_genre_id,
    profile_genres.profile_id as pivot_profile_id
FROM
    profiles
    INNER JOIN profile_genres ON profiles.id = profile_genres.profile_id
    LEFT JOIN (
        SELECT
            COUNT(id)
            visits,
            ip_address,
            visitorable_id,
            visitorable_type
        FROM
            visitors
        GROUP BY
            ip_address
    ) occurences ON
        visitors.visitorable_id = XXX AND
        visitors.visitorable_type = App\Profile)
WHERE
    profile_genres.genre_id = YYY AND
    profiles.deleted_at IS null

基本上,您从访问者那里执行一个选择语句,但您调用 occurences 的选择语句的响应。因此visitors.visitorable_id 不存在于临时表中,但occurences.visitorable_id 应该存在。

【讨论】:

    【解决方案2】:

    所以事实证明visitors 表在子查询中不可用,将visitors.visitorable_id 更改为occurences.visitorable_id 就成功了。

    $query->leftJoin(DB::raw('(SELECT COUNT(id) visits, ip_address, visitorable_id, visitorable_type FROM `visitors` GROUP BY ip_address) occurences'), function($join)
    {
        $join->on('occurences.visitorable_id', '=', 'profiles.id');
        $join->where('occurences.visitorable_type', '=', 'App\Profile');
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 2020-12-01
      • 1970-01-01
      • 2016-07-22
      • 2020-10-13
      • 1970-01-01
      • 2018-09-10
      相关资源
      最近更新 更多