I’ve seen references to recursion in SQL Server, but I’m using MySQL and require the result to be in a single column. If I have a table of relationships:
itemID1 | itemiD2
---------------
1 | 2
1 | 3
4 | 5
How do I select all IDs related to a single ID in either column? For example:
1 ==> 2,3
3 ==> 1,2
I tried self joins, but can’t get all related IDs in a single column. If there’s a better schema for this it’s not too late to change the table.
Thank you.
Please try this query:
This way you select relation in both ways (
unionprovides distinctiveness) as well as relation between joined items (also in both ways).