我有一张子ID表和父ID表。这些是单独的表格。我想创建一个将它们连接在一起的组合子表和父表。这两个表没有共同的ID。
Child Table
| childId |
| 1 |
| 2 |
Parent Table
| parentId |
| 3 |
| 4 |
Combined Child and Parent Table
| childId | parentId |
| 1 | 3 |
| 2 | 4 |
这两个表保证长度相同。 (虽然如果你能让它以任意长度工作,我会很高兴。)对的排序应该只是MySQL显示它的顺序。
如何获得结果表?
答案 0 :(得分:2)
您可以使用INNER JOIN
执行此操作,并使用ROW_NUMBER
为每个表分配ID。请参阅下面的查询:
SELECT childId,parentId FROM
(SELECT
@row_number1:=@row_number1+1 AS RowNumber1,
childId
FROM Child, (SELECT @row_number1:=0)AS x ORDER BY childId) AS A
INNER JOIN
(SELECT
@row_number2:=@row_number2+1 AS RowNumber2,
parentId
FROM Parent, (SELECT @row_number2:=0)AS y ORDER BY parentId) AS B
ON A.RowNumber1=B.RowNumber2