我有2张桌子。
account_receivables
有很多佣金
commissions
具有指向account_receivables_id
的外键。
我的应用程序的工作方式是account_receivables
获得插入内容时,它还基于account_receivables.amount
计算一组销售代表的佣金。
在2个月内,它随机错过了1个销售代表。因此,我想做的是找出所有错过佣金的account_receivables.id
。
是否有直接的SQL查询?这是一个Laravel应用,我没有运气尝试过以下方法:
$comms = DB::connection('mysqllocal')->table('commissions')
->select('account_receivables_id')
->whereNotIn('rep_group_number', [999999])
->groupBy('account_receivables_id')->get();
999999
是rep_group_number
答案 0 :(得分:0)
基本上,您正在寻找account_receivables
中所有在commissions
中没有对应记录的记录。
具有LEFT JOIN
的简单WHERE ... IS NULL
应该可以做到。
select ar.id
from account_receivables ar
left join commissions c on c.account_receivables_id = ar.id
where c.id is null
您没有在问题中提供完整的表格结构;上面的查询假定两个表之间的关系如下(如果需要,可以进行调整):
commissions.account_receivables_id -> account_receivables.id