2

what I would like to do is to add several sequence numbers to one single sql view in MySQL. Consider the following example table:

Folder 1, File 1
Folder 1, File 2
Folder 2, File 3
Folder 3, File 4
Folder 3, File 5
Folder 3, File 6

What I would like to get as a result would be:

1, Folder 1, File 1
2, Folder 1, File 2
1, Folder 2, File 3
1, Folder 3, File 4
2, Folder 3, File 5
3, Folder 3, File 6

I know how I can add one single sequence number to the whole view using variables but I have no idea how to solve my specific problem. Hopefully someone can help me with that.

Thanks a lot!

Cheers, Johannes

1 Answer 1

2

Instead of using one single variable you will need two, reset the row numbering whenever the group changes.

select folder, file,
  @r = case when @g = folder then @r+1 else 1 end SequenceNo,
  @g := folder
from (select @g:=null) g
cross join tbl
order by folder, file
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.