Can this be done with single SQL query
        Posted  
        
            by Ghostrider
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Ghostrider
        
        
        
        Published on 2010-05-27T23:41:12Z
        Indexed on 
            2010/05/28
            0:31 UTC
        
        
        Read the original article
        Hit count: 325
        
I'm using MySQL. I have a table
Type SubType
1 1
1 5
1 6
1 8
2 2
2 3
3 1
3 2
3 3
For each type there is some number of subtypes. For every subtype in a type there is a corresponding subtype in the next type:
(1,1) => (2,2)
(1,5) => (2,3)
(1,6) => (2,2)
(1,8) => (2,3)
(2,2) => (3,1)
(2,3) => (3,2)
In case you haven't seen the pattern, here it is: you sort both current and next types by subtype, then in the next type you get subtype in the same position as your current subtype in current type is. If there are more subtypes in the current type that in the next one, you warp around and start from the first subtype in the next type.
Is it possible to construct a query that takes current type and subtype and returns corresponding subtype in the next type?
© Stack Overflow or respective owner