有人知道如何提取有重復值的行嗎?
表名時間表
id|教員|教室|日|時
1 | Gerber | 205 | MW | 7:00:8:00
。2 | Gerber | 205 | MW | 7:00:8:00
。3 | Gerber | 205 | MW |7:00:8:00
。我試過的查詢只回傳1行(有計數)
$model = DB::table('schedules')
->選擇('id','instructor','room','day',[span class="hljs-string">'hours')
->whereIn('id', function($q){
$q->select('id'/span>)
->從('schedules')
->groupBy('room','hours')
->haveRaw('COUNT(*) > 1')。
})->paginate(10)。
有什么辦法嗎?
uj5u.com熱心網友回復:
表達你的邏輯的一種方法是將你的主表加入到一個子查詢中,這個子查詢找到重復出現的房間/時間組合:
$dupeSchedules = DB::table('schedules')
->選擇('房間', '小時')
->groupBy('room', 'hours')
->haveRaw('COUNT(*) > 1')。
$schedules = DB::table('schedules')
-> joinSub($dupeSchedules, 'dupe_schedules'/span>, function ($join) {
$join->on('schedules.room', '=', 'dupe_schedules.room') 。
$join->on('schedules.hours', '=', 'dupe_schedules.hours') 。
})->get()。
這將對應于以下原始的MySQL查詢:
SELECT s1.*
FROM附表s1
INNER JOIN
(
SELECT room, hours
FROM時間表
GROUP BY room, hours
HAVING COUNT(*) > 1)
) s2
ON s2.room = s1.room AND
s2.小時 = s1.小時。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/324234.html
標籤:
