Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have a table containing pairs of tags (say html tags) such as

id    tag                     tag_match
1     <div class="first">     1
2     <div class="second">    2
3     </div>                  2
4     </div>                  1
5     <div class="third">     3
6     </div>                  3

How to match the closing tag and opening tag in a category? UPDATE the table to set the corresponding values of tag_match column.

As the HTML tags, the rule is that each closing tag bongs to the last opening tag.

I think the most practical approach is to count user variable in an UPDATE with ORDER BY.

share|improve this question
I think this should be done not in SQL but in an outside application as it is essentially parsing. What should happen for example if there are more (or less) closing than opening divs? Or if the closing ones are in the beginning? Should the whole UPDATE operation fail? –  ypercube Jan 15 at 10:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.