【发布时间】:2020-02-01 15:06:52
【问题描述】:
我想在 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 分组的所有行都被选中,然后最后我们选择distinct user_ids 来自给定的一组用户 ID。
此查询的总体目标是获取所需列值(user_id's in this case) > 1...的所有行...
例如:
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
上面的原始查询对我有用,但现在我正在编写一个 api,我想知道如何在 Eloquent 中复制它。
请帮忙!
【问题讨论】:
标签: php eloquent laravel-5.6 eloquent-relationship