I am trying to create a lookup table where the order of the elements matters. My lookup table has the following structure
id table1id table2id
1 1 1
2 1 2
3 1 3
4 2 2
5 2 1
6 2 3
My goal is to find table1id based off the table2ids. So an example query I run is
Select table1id
from junctionTable
where table2ids in (1,2,3)
group by table1id
Having count(table1id) = 3
This works, however, it will return both Table1Ids 1 and 2. I only want the Table1ID that has 1,2,3 in that specific order so it should return only TableId =1.
Putting an order column on the table works, however it makes the query a little more difficult as the number of items increase.
select table1id from junctionTable
where table2id =1 and order =1 and
table2id = 2 and order =2
etc...
Is there anything else I can do that I am not thinking of? Or what would be the best way to handle this situation?
The question originated from me trying to normalize tables. See Normalize a table with tightly coupled data for reference