Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group only the adjacent rows

Tags:

sql

mysql

hi i have a table like this:

notifies

id,user_id
1,3
2,3
3,4
4,5
5,6
6,3
7,4
8,4
9,3
10,3

I have to create a query that group only the adjacent rows

So, the result of this example should be:

user_id
3
4
5
6
3
4
3

How can i do that? thanks

like image 767
Luca Romagnoli Avatar asked Oct 21 '25 17:10

Luca Romagnoli


1 Answers

SELECT  user_id
FROM    notifies n
WHERE   NOT
        (
        SELECT  user_id
        FROM    notifies ni
        WHERE   ni.id < n.id
        ORDER BY
                id DESC
        LIMIT 1
        ) <=> user_id
like image 85
Quassnoi Avatar answered Oct 23 '25 07:10

Quassnoi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!