繁体   English   中英

雄辩地构建SQL查询

[英]Building SQL query in eloquent

我该如何用雄辩的话在Laravel 4.2中编写此SQL查询?

SELECT *
FROM participants
WHERE user_id IN (1, 2) 
GROUP BY convo_id
HAVING count(DISTINCT user_id) = 2
AND count(DISTINCT convo_id) = 1

我已经尝试过了

Participant::whereIn('user_id', $participantIds)
                    ->groupBy('convo_id')
                    ->havingRaw('count(DISTINCT user_id) = '. sizeof($participantIds))
                    ->whereRaw('count(DISTINCT convo_id) = 1')
                    ->get();

但是我得到这个错误

SQLSTATE[HY000]: General error: 1111 Invalid use of group function (SQL: select * from `participants` where `user_id` in (1, 2, 4) and count(DISTINCT convo_id) = 1 group by `convo_id` having count(DISTINCT user_id) = 3)

我的错。 whereRaw也应该具有haw。

Participant::whereIn('user_id', $participantIds)
                    ->groupBy('convo_id')
                    ->havingRaw('count(DISTINCT user_id) = '. sizeof($participantIds))
                    ->havingRaw('count(DISTINCT convo_id) = 1')
                    ->get();

最后尝试groupBy

Participant::whereIn('user_id', $participantIds)                        
                    ->havingRaw('count(DISTINCT user_id) = '. sizeof($participantIds))
                    ->whereRaw('count(DISTINCT convo_id) = 1')
                    ->groupBy('convo_id')
                    ->get();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM