从 2 个表创建分层数据

Create hierarchical data from 2 tables

我有两个这样的表:

ProdId ProdName
1      Mike
2      Carol
3      John
4      Marc

OrdId OrdName ProdId
100     Pizza   1
101     Subs    1
102     Calzone 2
102     Sausage 2
102     Beef    2
105     Pork    4
106     Coke    4

我需要一个查询来填充这样的结果。层次结构应该是 parent 和它们的 child,然后是 parent,然后是 child。

MarkerId  MarkerName MarkerParentId
1         Mike       NULL
100       Pizza      1
101       Subs       1
2         Carol      NULL
102       Calzone    2
103       Sausage    2
104       Beef       2
3         John       NULL
4         Marc       NULL
105       Pork       4
106       Coke       4

请帮忙。谢谢

与递归无关。基本上你可以用 UNION

得到你想要的
SELECT ProdId as MarkerId, ProdName as MarkerName, NULL as MarkerParentId from t1
UNION ALL 
SELECT OrdId as MarkerId, OrdName as MarkerName, ProdId MarkerParentId from t2
ORDER BY MarkerId, MarkerParentId