这是方案
表:用户
id | name | grade | subject
---- | ------|--------|------
1 | Mark | a | science
2 | Earl | a | english
3 | John | c | english
4 | Mike | d | science
5 | Matt | e | english
我想要做的是填写非重复或非重复成绩但仅限英语科目的成绩
所以它应该只显示
c
e
我到目前为止
控制器:
$grades = user::select('grade', DB::raw('COUNT(grade) as gradecount'))
->where('subject', 'english')
->groupBy('grade')
->having('gradecount', '=',1)
->get();
刀片:
@foreach ($grades as $grade)
{{ $grade }}
@endforeach
但是,此查询提供了此
a
c
e
答案 0 :(得分:2)
你会想做这样的事情:
SELECT grade
FROM Users
WHERE id IN (
SELECT id
FROM Users
GROUP BY grade
HAVING COUNT( grade ) = 1
) AND subject = 'english'`
“查询”构建器具有您应该能够使用的 - > whereIn。太懒了测试,但这样的事情可能有用。
$ids = user::select('id')
->groupBy('grade')
->having('gradecount', '=',1)
->get();
$grades = user::select('grade')
->whereIn('id', $ids)
->where('subject', 'english')
->get();