代码之家  ›  专栏  ›  技术社区  ›  ChrisBratherton

SQLSTATE[42S22]:未找到列:1054未知列(似乎试图使用ID作为列名)

  •  1
  • ChrisBratherton  · 技术社区  · 7 年前

    因此,我最近一直在与拉雷维尔的一个范围作斗争,我得到了以下错误:

    “访客。“on子句”中的“visitorable_id”(SQL:select) profiles .*, profile_genres . genre_id pivot_genre_id profile\u流派 . profile_id pivot_profile_id 从…起 配置文件 profile\u流派 在…上 . id . profile\u id 左连接(选择COUNT(id)访问, visitors 访客 . visitorable_id db885a18-f0b7-4f55-9d93-743fbb5d9c94 访客 . visitorable_type =应用程序\配置文件,其中 profile\u流派 类型id 配置文件 . 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();
            });
        }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Jeffrey    7 年前
    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
  •  0
  •   ChrisBratherton    7 年前

    结果是 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');
    });