【问题标题】:Convert This Sql to Eloquent将此 Sql 转换为 Eloquent
【发布时间】:2018-08-13 10:27:03
【问题描述】:

如何使用 Eloquent 创建这个查询?

SELECT
    * 
FROM
    `contents` 
WHERE
    `id` NOT IN (
    SELECT
        `id` 
    FROM
        `contents` AS `laravel_reserved_0` 
    WHERE
        `laravel_reserved_0`.`parent_id` IN ( 
            SELECT
                `id` 
            FROM
                `contents` AS `laravel_reserved_1` 
            WHERE
                `laravel_reserved_1`.`type` IN ( 'pro' )
            ) 
    ) 
AND `is_active` = 1 
AND `type` IN ( 'stars', 'video', 'article' )
GROUP BY
    `slug` 
ORDER BY
    `created_at` DESC 
    LIMIT 10

【问题讨论】:

标签: mysql laravel eloquent


【解决方案1】:

您可以执行多个查询,嵌套查询分开。

$typeIDs = Content::whereIn('type', ['pro'])->pluck('id');

$parentIDs = Content::whereIn('parent_id', $typeIDs)->pluck('id');

$contents = Content::whereNotIn('id', $parentIDs)
    ->where('is_active', 1)
    ->whereIn('type', ['stars', 'video', 'article'])
    ->groupBy('slug')
    ->orderBy('created_at', 'desc')
    ->limit(10)
    ->get();

除此之外,如果您愿意,您还可以将创建的变量替换为实际查询本身。

【讨论】:

    猜你喜欢
    • 2017-03-09
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 2021-09-11
    • 2021-10-04
    • 1970-01-01
    • 2018-09-28
    相关资源
    最近更新 更多