gpt4 book ai didi

php - SQLSTATE[42S22] : Column not found: 1054 Unknown column (Seems to be trying to use ID as column name)

转载 作者:行者123 更新时间:2023-11-28 23:11:19 24 4
gpt4 key购买 nike

所以我最近一直在与 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;
}

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

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 不存在于临时表中,但 occurences.visitorable_id 应该存在。

关于php - SQLSTATE[42S22] : Column not found: 1054 Unknown column (Seems to be trying to use ID as column name),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45899064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com