Answer the question
In order to leave comments, you need to log in
How to group only consecutive records from a database?
There is a revision table (based on venturecraft revisionable):
+----+-----------------+---------+---------------------+
| id | revisionable_id | user_id | created_at |
+----+-----------------+---------+---------------------+
| 6 | 1 | 1 | 2017-03-15 23:59:40 |
| 5 | 1 | 1 | 2017-03-15 23:56:40 |
| 4 | 3 | 8 | 2017-03-15 23:55:40 |
| 3 | 4 | 3 | 2017-03-15 23:55:40 |
| 2 | 1 | 1 | 2017-03-15 23:15:40 |
| 1 | 1 | 1 | 2017-03-15 22:59:40 |
+----+-----------------+---------+---------------------+
Answer the question
In order to leave comments, you need to log in
UPD: not 100% satisfying, but working approach
//шаманим постраничную навигацию вручную
$paginate = 10;
$page = Input::get('page', 1);
$offSet = ($page * $paginate) - $paginate;
//забираем все записи и сортируем их
$records = Revision::select(
'revisionable_id',
'revisionable_type',
'user_id',
DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d') as created"),
DB::raw('count(*) as total')
)
->groupBy('revisionable_id','revisionable_type','user_id','created')
->orderBy('created','desc')
->get();
//выбираем записи для нужной страницы
if($page==1){
$sliced = collect($records)->slice(0,$paginate)->all();
}else{
$sliced = collect($records)->slice($offSet, $paginate)->all();
}
//отправляем все в view
$revisions = new Paginator($sliced, count($records), $paginate, $page);
//не забываем поправить путь ссылок в постраничной навигации
$revisions = $revisions->setPath('/revisions');
SELECT
user_id, revisionable_id, COUNT(*)
FROM
table
GROUP BY
CONCAT(user_id, "-", revisionable_id)
I can only advise a stored procedure, which will create the table you need and add an additional column, which will be incremented when the user_id changes. Then you can make a group by on a new column and sort by it. Those. you get something like
SELECT `t`.*, COUNT(*) AS `count` FROM my_func() AS `t` GROUP BY `newcol` ORDER BY `newcol`;
And my_func - should return the table by full consecutive search of data search.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question