【问题标题】:Write custom SQL query for multiple counts为多个计数编写自定义 SQL 查询
【发布时间】:2018-10-24 04:52:46
【问题描述】:

我在 MySQL 中有一个表,包含group_nameusername,像这样:

ID|group_name|username
----------------------
1 |    A     | user1
----------------------
2 |    B     | user2
----------------------
3 |    C     | user1

...

我有一些看起来像这样的逻辑表达式:

(A & B) || C

这意味着,如果我正在搜索某个用户,该用户应该在 A 组和 B 组中,或者在 C 组中。

我必须在 Laravel 中使用自定义表达式检查用户,我的查询将如下所示:

return DB::table('assigned_groups')->where($expression)->where('username', $username)->count();

我猜$expression 是我用原始 SQL 编写的逻辑表达式。而且我必须检查是否可以找到一些$username 至少分配给需要的组一次。

现在我只有一段 $expression 的伪代码,如下所示:

select count(*)
having (
    (count(select(*) where group_name = A) > 0 
    and count(select(*) where group_name = B) > 0)
    or count(select(*) where group_name = C) > 0
)

如何正确编写这个表达式?我应该如何更改我的 Laravel 查询和 $expression 本身?

UPD:现在我的 SQL 看起来像这样,几乎是这样的

SELECT count(*) FROM `assigned_groups`
where username = 'user1'
having (
    (count(case group_name when 'A' then 1 else null end) > 0 
    and count(case group_name when 'B' then 1 else null end) > 0)
    or count(case group_name when 'C' then 1 else null end) > 0
)

【问题讨论】:

标签: mysql sql laravel laravel-5.2 logic


【解决方案1】:

您可以使用havingRaw 编写原始表达式

DB::table('assigned_groups')
->where('username', $username)
->havingRaw("(count(case group_name when 'A' then 1 else null end) > 0 
    and count(case group_name when 'B' then 1 else null end) > 0)
    or count(case group_name when 'C' then 1 else null end) > 0")
->count();

使用sum()或更短

DB::table('assigned_groups')
->where('username', $username)
->havingRaw("(sum(group_name ='A') > 0 and sum(group_name = 'B') > 0) or sum(group_name = 'C') > 0")
->count();

【讨论】:

    【解决方案2】:

    试试这个:

    return DB::table('assigned_groups')
        ->where('username', $username)
        ->andWhere(function($query) use ($groupAandB, $groupC) {
            $query->whereIn('group_name', $groupAandB)
                  ->orWhereIn('group_name', $groupC);
        })
        ->count();
    

    我实际上不确定是否有orWhereIn 方法,但是这个结构应该给你一个很好的起点。

    【讨论】:

      【解决方案3】:

      试试这个:

      $users = DB::select("select count() from user where username='$username' and (username in (select username from user where group_name in ('A','B') count() >1 group by username) or group_name ='C')");

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-14
        • 1970-01-01
        • 1970-01-01
        • 2020-04-05
        • 2018-07-03
        • 2015-09-17
        • 1970-01-01
        • 2019-07-25
        相关资源
        最近更新 更多