Laravel Sail - SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;

问题描述 投票:0回答:1

有人知道为什么这个查询返回错误吗?我正在使用 Laravel Sail 和 Mysql 版本:8.0.32,它应该可以工作:

Property::select(DB::raw("
            properties.id,
            properties.company_id,
            properties.title,
            properties.slug,
            properties.neighborhood,
            properties.city,
            properties.address,
            properties.number,
            properties.property_purpose,
            properties.property_type,
            properties.total_area,
            properties.usefull_area,
            properties.bedrooms,
            properties.bathrooms,
            properties.garage,
            properties.sell,
            properties.rent,
            properties.administration_fee,
            properties.yearly_tax,
            property_highliteds.hightlight,
            companies.property_show_number,
            ROW_NUMBER() OVER (PARTITION BY properties.company_id ORDER BY property_highliteds.hightlight DESC, properties.created_at DESC) as row_number
        "))
            ->leftJoin('property_highliteds', 'properties.id', '=', 'property_highliteds.property_id') // Relacionamento com os destaques
            ->leftJoin('companies', 'properties.company_id', '=', 'companies.id')   // Relacionamento com as empresas
            ->orderBy('row_number')  // Ordena pela intercalação dos imóveis
            ->with([
                'images_thumb:id,property_id,url',  // Carrega apenas a URL das imagens
                'company:id,property_show_number',  // Carrega o campo necessário de Company
                'highlight:id,property_id,hightlight' // Carrega o destaque via BelongsTo
            ])
            ->paginate($perPage);

错误是:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC) as row_number from `properties` left join `companies` on `propert' at line 23

用 ChatGpt 和谷歌搜索尝试了几乎所有的事情。我的mysql是正确的。

mysql laravel eloquent
1个回答
0
投票

我的mysql是正确的。

只是重复其他人所说的,您需要发布此代码实际生成的实际 SQL 查询(

->toSQL()
),否则您将永远无法确定(并且我们可能无法提供帮助,因为一开始乍一看这段代码似乎确实没问题)。

© www.soinside.com 2019 - 2024. All rights reserved.