laravel - How to select duplicate rows and group by two columns -
i have following table:
id|user_id|group_id|subject |book_id 1| 2 |3 |history |1 2| 4 |3 |history |1 3| 5 |3 |art |2 4| 2 |3 |art |2 5| 1 |4 |sport |5
i list rows group 3(id) have duplicate rows same subject_id , book_id. subject , book_id determine 2 or more rows duplicate.
i distinct results this:
|subject |book_id| |history |1 | |art |2 |
using either query builder or eloquent
a sql query desired result may
select subject, book_id table1 group_id = 3 group subject, book_id having count(*) > 1
here sqlfiddle demo
now same using laravel query builder
$duplicates = db::table('table1') ->select('subject', 'book_id') ->where('group_id', 3) ->groupby('subject', 'book_id') ->havingraw('count(*) > 1') ->get();
Comments
Post a Comment