通过顺序添加所有表的数据将两个表连接成一个

时间:2017-04-13 08:23:29

标签: sql join sql-server-2012

我在使用不同数据连接三​​个表时遇到问题。 假设我有 table1 table2 ,如:

table3
------
ID1    ID2
---    ---
1      102
2      103

我需要将这两个表加入 table3 中:

table3 :
ID1   ID2
---   ---
1     102
2     102
1     103 
2     103

我在table1和table2中应用交叉连接但我得到了:

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            View v = snapHelper.findSnapView(mRecyclerView.getLayoutManager());
            float distanceFromSnap = abs(snapHelper.calculateDistanceToFinalSnap(mRecyclerView.getLayoutManager(), v)[0]);

            animate(v, distanceFromSnap);
        }
    });

private void animate(View v, float distanceFromSnap) {
    View image = v.findViewById(R.id.item_active_image);

    float distanceStartAnim = 250;
    float scaleStart = 0.4f;
    float scaleEnd = 1.0f;

    float newScale = scaleStart;

    if (distanceFromSnap <= distanceStartAnim) {
        float percentage = 1.0f - distanceFromSnap/distanceStartAnim;
        newScale = percentage * (scaleEnd - scaleStart) + scaleStart;
    }

    image.setScaleX(newScale);
    image.setScaleY(newScale);
}

2 个答案:

答案 0 :(得分:2)

如果您只是按ID为每个表排序,然后将第一行与第一行匹配 - 以下内容应该有效。

Select T1.ID1
     , T2.ID2
from (Select ID1, row_number() over (order by ID1) rownum from Table1) T1
inner join (Select ID2, row_number() over (order by ID2) rownum from Table2) T2
    on T1.rownum = T2.rownum

它为每个具有行号的表创建子查询,然后在行号上创建内部联接。

答案 1 :(得分:0)

如果您的ID并非始终处于顺序状态,请使用此:

SELECT t1.ID1, T2.ID2
FROM (SELECT ID1, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) rn FROM table1 ) t1
INNER JOIN (SELECT ID2, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) rn FROM table2) t2
    ON t1.rn = t2.rn