SQLSTATE[42S22]: Column not found: 1054 Unknown column (似乎试图使用 ID 作为列名)
SQLSTATE[42S22]: Column not found: 1054 Unknown column (Seems to be trying to use ID as column name)
所以我最近一直在与 Laravel 中的范围作斗争,我收到以下错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column
'visitors.visitorable_id' in 'on clause' (SQL: 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
=
db885a18-f0b7-4f55-9d93-743fbb5d9c94
and
visitors
.visitorable_type
= App\Profile where
profile_genres
.genre_id
= 038ba398-8998-4cd6-950c-60b4a6c401cc and
profiles
.deleted_at
is null)
这是导致此错误的查询:
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;
}
编辑
根据要求,我的访客 table 迁移:
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();
});
}
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
基本上,您从访问者那里做一个 select 声明,但是您调用 occurences
来自 select 声明的响应。因此 visitors.visitorable_id
在临时 table 中不存在,但 occurences.visitorable_id
应该存在。
所以事实证明 visitors
table 在子查询中不可用,将 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');
});
所以我最近一直在与 Laravel 中的范围作斗争,我收到以下错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'visitors.visitorable_id' in 'on clause' (SQL: select
profiles
.*,profile_genres
.genre_id
aspivot_genre_id
,profile_genres
.profile_id
aspivot_profile_id
fromprofiles
inner joinprofile_genres
onprofiles
.id
=profile_genres
.profile_id
left join (SELECT COUNT(id) visits, ip_address, visitorable_id, visitorable_type FROMvisitors
GROUP BY ip_address) occurences onvisitors
.visitorable_id
=db885a18-f0b7-4f55-9d93-743fbb5d9c94
andvisitors
.visitorable_type
= App\Profile whereprofile_genres
.genre_id
= 038ba398-8998-4cd6-950c-60b4a6c401cc andprofiles
.deleted_at
is null)
这是导致此错误的查询:
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;
}
编辑 根据要求,我的访客 table 迁移:
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();
});
}
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
基本上,您从访问者那里做一个 select 声明,但是您调用 occurences
来自 select 声明的响应。因此 visitors.visitorable_id
在临时 table 中不存在,但 occurences.visitorable_id
应该存在。
所以事实证明 visitors
table 在子查询中不可用,将 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');
});