简体   繁体   中英

Select records only if previous row coloumn is different SQL in MYSQL

I have a problem with MySql, that I can't find if is possible to solve with MySql.

I have this table for example:

|cod | value    |
| 1  |  value1  |
| 1  |  value2  |
| 2  |  value3  |
| 1  |  value4  |
| 5  |  value5  |
| 5  |  value6  |
| 2  |  value7  |

The result that I would like:

|cod | value    |
| 1  |  value1  |
| 2  |  value3  |
| 1  |  value4  |
| 5  |  value5  |
| 2  |  value7  |

So get row only if it is has cod different from previous record.

So my question is: Is possibile doing this with Mysql? Is there any way for know the previous row value in MySQL?

I'have solved in this way, but I don't know if it is the best way:

set @last := 'NULL';
Select cod, test.value, @last := cod as previous
from test
where @last != cod

Some one more skilled in SQL could suggest me if it is a valid method?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM