时间戳转换器

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

日期:2023-02-16     浏览:242    
【中文标题】使用 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]

【讨论】:

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

相关文章

查询中的Firebase多个WHERE子句[重复]

{...只能提取机场或到达日期相同的数据,而不是两者(只能使用一次equalTo())。这是我当前的java代码的样子:Firebase}

oracle检索数据(查询数据select语句)

{...数据,检索数据可以通过select语句来实现,该语句由多个子句组成,通过这些子句完成筛选、投影和连接等各种数据操作,最终得到想要的结果。语法:  select[distinct|all] columns|*  [intotable_name]  fromtables|views|otherselect...}

如何在 Maximo Anywhere 中编写 OSLC 查询 where 子句以评估 somevalue < now()

{】如何在MaximoAnywhere中编写OSLC查询where子句以评估somevalue<now()【英文标题】:HowtowriteOSLCquerywhereclauseinMaximoAnywheretoevaluatesomevalue<now()【发布时间】:2017-01-3119:28:28【问题描述】:我正在配置工作执行。在检索资产或位置的过...}

6.过滤数据

{学习目标使用where子句where子句操作符检查单个值不匹配检查范围检查一.使用where子句数据库当中,通常会根据特定操作或报告的提取表需要数据的子集。 只检索所需数据,需要指定搜索条件searchcriteria(过滤条件filtercriteria)...}

具有嵌套多个条件的 WHERE 子句

{】具有嵌套多个条件的WHERE子句【英文标题】:WHEREclausewithnestedmultipleconditions【发布时间】:2012-08-2421:38:27【问题描述】:我想检索带有WHERE子句中条件的数据。这是我的表格:NameLocationAge----------------------AAABhuj24BBBMumbai22CCCBhuj18DD...}

如何从 Android SQLite 查询的动态 ArrayList 编写 WHERE IN 子句

{】如何从AndroidSQLite查询的动态ArrayList编写WHEREIN子句【英文标题】:HowtowriteWHEREINclausefromadynamicArrayListforAndroidSQLitequery【发布时间】:2011-10-3011:01:16【问题描述】:如何在AndroidSQLite查询中编写wherein子句?检索单个客户的功能public...}

挽救数据库性能的30条黄金法则(代码片段)

{...及到的字段上需要建立索引。应该尽量避免在where子句中使用否定的操作符,如不等于(!=或<>)、否则数据库引擎将放弃使用索引而进行全表扫描。在尽量避免在where子句中使用或(or)作为连接条件,否则数据库引擎将放...}

在 WHERE 子句中使用短路会提高速度吗

{】在WHERE子句中使用短路会提高速度吗【英文标题】:WillUsingShort-CircuitinginWHEREClauseImproveSpeed【发布时间】:2020-08-0523:08:36【问题描述】:用例:我将使用SQLServer从一个大表(1,000,000+行)中检索值,其中许多不同的列可以用作过...}

oracle各部分执行顺序

{...最右。3、GROUPBY:执行顺序从左往右分组,最好在GROUPBY前使用WHERE将不需要的记录在GROUPBY之前过滤掉。4、HAVING子句:消耗资源。尽量避免使用,HAVING会在检索出所有记录之后}

里面有查询的 MySQL 函数

{...ueryinsideit【发布时间】:2017-07-1320:55:17【问题描述】:我使用这个mysql函数通过传递三个参数来检索列数据的总和。无论where子句如何,函数都会返回整列的总和。我的意思是函数内部的查询没有where子句,并且在没有函数的情况...}

Copyright ©2021 时间戳转换器 小常识 114pp | 陕ICP备18005036号