我想在Laravel的Eloquent中执行以下原始SQL查询:
SELECT distinct user_id FROM order_payment WHERE user_id IN (SELECT user_id FROM order_payment where payment_status = 'paid' GROUP BY user_id HAVING COUNT(*) > 1);
[这里我将where in
与属性user_id
一起使用,以便选择同一表(order_payment
)中具有payment_status=paid
并按具有user_id
的属性count > 1
分组的所有行,然后最终选择[ C0]来自给定的用户ID集。
此查询的总体目标是获取所需列值(distinct user_ids
)> 1 ...]的所有行。
例如:
user_id's in this case
上面的原始查询对我有用,但是现在我正在编写一个api,我想知道如何在Eloquent中复制它。
请帮助!
<<所以可以使用we have 3 rows > A, B, C
row A has user_id = 1;
row B has user_id = 1;
row C has user_id = 2;
result should have [user_id: 1] as the count becomes greater than 1
和groupBy
方法完成这种查询的雄辩方式