使用 where 子句检索数据时,无论如何要检索计数 0 吗? (Laravel 雄辩)

Posted

技术标签:

【中文标题】使用 where 子句检索数据时,无论如何要检索计数 0 吗? (Laravel 雄辩)【英文标题】:Is there anyway to retrieve count 0 when retrieve data using where clause? (Laravel Eloquent) 【发布时间】:2022-01-12 15:46:23 【问题描述】:

我目前正在使用 group by 检索数据,以获得相同的宝藏名称、找到的总数和相同的宝藏数量(上限)。使用下面的代码,我可以得到所有的宝物数据,但是当宝物被完全认领时,它无法显示total_left为0。

*claimed 列是一个布尔值,其中 0 尚未被声明。 *cap 是同一地点的总宝藏

查询

$treasure_hunt_data = TreasureHunt::where('claimed', '0')
                        ->selectRaw(" treasure_name, count(claimed) as total_left, cap")
                        ->groupBy(['treasure_name', 'cap'])
                        ->get();

数据

[
    "treasure_name":"Location A","total_left":5,"cap":5,
    "treasure_name":"Location B","total_left":2,"cap":2,
    "treasure_name":"Location C","total_left":2,"cap":2,
    "treasure_name":"Location D","total_left":10,"cap":10
]

所需数据

[
    "treasure_name":"Location A","total_left":5,"cap":5,
    "treasure_name":"Location B","total_left":2,"cap":2,
    "treasure_name":"Location C","total_left":2,"cap":2,
    "treasure_name":"Location D","total_left":10,"cap":10,
    "treasure_name":"Location E","total_left":0,"cap":1
]

数据库数据

迁移详情

   Schema::create('treasure_hunts', function (Blueprint $table) 
        $table->id();
        $table->string('treasure_name');
        $table->boolean('claimed')->default(0);
        $table->string('cap');
        $table->timestamps();
    );

【问题讨论】:

请添加模型和迁移详细信息以及所需输出的示例数据,以便我们了解您的数据库是什么样的。 @miken32,我已经添加了迁移细节和数据库数据 请勿发布代码图片 【参考方案1】:

您可以使用 CTE(公用表表达式)和 DB::select 语句来做到这一点。例如:

$treasure_hunt_data = DB::select("
    with treasure_data as (
        select treasure_name, count(claimed) as total_left, cap
        from treasure_hunt
        where claimed = 0 
        group by treasure_name, cap
    )
    select * from treasure_data where total_left = 0
");

这会将结果集作为数组返回。

【讨论】:

使用 where total_left = 0; 不会有数据返回没有 where total_left = 0;我们将得到 ["treasure_name":"Location A","total_left":5,"cap":5,"treasure_name":"Location B","total_left":2,"cap":2, "treasure_name":"位置 C","total_left":2,"cap":2,"treasure_name":"位置 D","total_left":10,"cap":10] 根据需要编辑 CTE,仅作为示例。【参考方案2】:

以下是数据库中的实际记录:

["id":1,"treasure_name":"Location A","claimed":1,"cap":"10","created_at":null,"updated_at":null,
"id":2,"treasure_name":"Location B","claimed":1,"cap":"220","created_at":null,"updated_at":null,
"id":3,"treasure_name":"Location C","claimed":1,"cap":"42","created_at":null,"updated_at":null,
"id":4,"treasure_name":"Location D","claimed":0,"cap":"23","created_at":null,"updated_at":null,
"id":5,"treasure_name":"Location A","claimed":1,"cap":"233","created_at":null,"updated_at":null]

试试这个查询来检索数据:

$treasure_hunt_data = TreasureHunt::select(
            'treasure_name',
            \DB::raw('sum(case when claimed = 1 then 1 else 0 end) as total_left'), 
            \DB::raw('sum(cap) as cap')
        )
        ->groupBy('treasure_name')
        ->get();

以下是上述查询的结果:

["treasure_name":"Location A","total_left":"2","cap":243,
"treasure_name":"Location B","total_left":"1","cap":220,
"treasure_name":"Location C","total_left":"1","cap":42,
"treasure_name":"Location D","total_left":"0","cap":23]

【讨论】:

是的,这项工作,但需要对其进行一些修改

以上是关于使用 where 子句检索数据时,无论如何要检索计数 0 吗? (Laravel 雄辩)的主要内容,如果未能解决你的问题,请参考以下文章

1.《SQL必知必会》--(更新中...)

从数据表中检索信息

sql 从sys.columns中检索where子句

MySQL必知应会-第6章-过滤数据

过滤数据

组合字符串时方法语法中的 Where 子句失败