I have a simple table and I need to identified groups of four rows (the groups aren't consecutives), but each rows of each row has a +1 in the value. For example:
---------------------- | language | id | ---------------------- | C | 16 | | C++ | 17 | | Java | 18 | | Python | 19 | | HTML | 65 | | JavaScript | 66 | | PHP | 67 | | Perl | 68 | ----------------------
I want to add a column that indicates the group or set, how is possible to get this output using MySQL?:
---------------------------- | language | id | set | ---------------------------- | C | 16 | 1 | | C++ | 17 | 1 | | Java | 18 | 1 | | Python | 19 | 1 | | HTML | 65 | 2 | | JavaScript | 66 | 2 | | PHP | 67 | 2 | | Perl | 68 | 2 | ----------------------------
Note that in this examples is only 2 sets (it could be 1 or more sets) and they didn't start in 16 (such values are not knowledged, but the restriction is that each id value of each row has this form n, n+1, n+2 and n+3).
I've been investigating about Gaps & Islands problem but didn't figure how to solve it by using their solutions. Also I search on stackoverflow but the closest question that I found was How to find gaps in sequential numbering in mysql?
Thanks