Carlo Jimenez
Carlo Jimenez

Reputation: 131

Mysql overwrite next rows in select

i have the following table

id name flag
1 carl 0
2 mark 1
3 steve 1

what I want to do is the first row would override the succeeding row value for flag so the result would be something like this

id name new_inherited_flag original_flag
1 carl 0 0
2 mark 0 1
3 steve 0 1

how do i achieve this?

Upvotes: 0

Views: 50

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522094

You could just use a subquery to find the earliest flag value:

SELECT id, name,
       (SELECT flag FROM yourTable ORDER BY id LIMIT 1) AS new_inherited_flag,
       flag AS original_flag
FROM yourTable
ORDER BY id;

Upvotes: 1

Related Questions