为多个计数编写自定义 SQL 查询

Write custom SQL query for multiple counts

我在 MySQL 中有 table,包含 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
)

您可以使用 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();

试试这个:

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 方法,但这个结构应该给你一个很好的起点。

试试这个:

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