Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何在SQL Server中交叉连接两个表,仅重复某些值_Sql Server_Join - Fatal编程技术网

Sql server 如何在SQL Server中交叉连接两个表,仅重复某些值

Sql server 如何在SQL Server中交叉连接两个表,仅重复某些值,sql-server,join,Sql Server,Join,在SQL Server中,我有两个表。第一个是: id Value 1 Green 2 Yellow 3 Red id1 Value1 id2 Value2 1 Green 1 John 2 Yellow 2 Paul 3 Red 3 George 1 Green 4 Ringo 2 Yellow 5 Mick 3 Red 6 Keith 1 Green 7 Ronnie 第二个是 id Value 1 Joh

在SQL Server中,我有两个表。第一个是:

id Value
1  Green
2  Yellow
3  Red
id1 Value1 id2 Value2
1   Green  1   John
2   Yellow 2   Paul
3   Red    3   George
1   Green  4   Ringo
2   Yellow 5   Mick
3   Red    6   Keith
1   Green  7   Ronnie
第二个是

id Value
1  John
2  Paul
3  George
4  Ringo
5  Mick
6  Keith
7  Ronnie
如何连接这两个表,例如结果数据集,如下所示:

id Value
1  Green
2  Yellow
3  Red
id1 Value1 id2 Value2
1   Green  1   John
2   Yellow 2   Paul
3   Red    3   George
1   Green  4   Ringo
2   Yellow 5   Mick
3   Red    6   Keith
1   Green  7   Ronnie

在where子句中需要一个语句 表示table1.id%2=table2.id%2


模运算符%将第二个表的奇数值与绿色匹配,偶数值与黄色匹配。

您需要where子句中的语句 表示table1.id%2=table2.id%2

模运算符%将第二个表的奇数值与绿色匹配,偶数值与黄色匹配。

尝试以下操作:

Select *
From (Select
  t.*,
  2 - (Row_number() over (order by id) % 2) idx
From second_table t) t inner join first_table t2 on t2.id = t.idx;
试试这个:

Select *
From (Select
  t.*,
  2 - (Row_number() over (order by id) % 2) idx
From second_table t) t inner join first_table t2 on t2.id = t.idx;

以下是一个应与问题的更新版本一起使用的示例:

declare @colors table (
    id int not null,
    value nvarchar(15) not null
)
declare @people table (
    id int not null,
    name nvarchar(15) not null
)

insert into @colors (id, value) values (1, 'Green'),(2,'Yellow'),(3,'Red')
insert into @people (id, name) values (1, 'John'),(2,'Paul'),(3,'George'),(4,'Ringo'),(5,'Mick'),(6,'Keith'),(7,'Ronnie')

----

select csub.id, csub.value, psub.id, psub.name
from 
    (
        select id, name, 
            (row_number() over (order by id)) % (select count(*) from @colors) as rnum
        from @people
    ) as psub
    join (
        select id, value, 
            ((row_number() over (order by id)) % (select count(*) from @colors)) as rnum
        from @colors
    ) csub on psub.rnum = csub.rnum
order by psub.id
注意:即使两个表中的实际ID值存在间隙,这也有效。比如:

insert into @colors (id, value) values (1, 'Green'),(17,'Yellow'),(33,'Red'),(47,'Black)
insert into @people (id, name) values (1, 'John'),(2,'Paul'),(3,'George'),(7,'Ringo'),(15,'Mick'),(16,'Keith'),(37,'Ronnie')
无论颜色表中有多少行,它都可以工作。使用上面的示例输出,ID范围中有间隙:

+----+--------+----+--------+
| id | value  | id |  name  |
+----+--------+----+--------+
|  1 | Green  |  1 | John   |
| 17 | Yellow |  2 | Paul   |
| 33 | Red    |  3 | George |
| 47 | Black  |  7 | Ringo  |
|  1 | Green  | 15 | Mick   |
| 17 | Yellow | 16 | Keith  |
| 33 | Red    | 37 | Ronnie |
+----+--------+----+--------+

以下是一个应与问题的更新版本一起使用的示例:

declare @colors table (
    id int not null,
    value nvarchar(15) not null
)
declare @people table (
    id int not null,
    name nvarchar(15) not null
)

insert into @colors (id, value) values (1, 'Green'),(2,'Yellow'),(3,'Red')
insert into @people (id, name) values (1, 'John'),(2,'Paul'),(3,'George'),(4,'Ringo'),(5,'Mick'),(6,'Keith'),(7,'Ronnie')

----

select csub.id, csub.value, psub.id, psub.name
from 
    (
        select id, name, 
            (row_number() over (order by id)) % (select count(*) from @colors) as rnum
        from @people
    ) as psub
    join (
        select id, value, 
            ((row_number() over (order by id)) % (select count(*) from @colors)) as rnum
        from @colors
    ) csub on psub.rnum = csub.rnum
order by psub.id
注意:即使两个表中的实际ID值存在间隙,这也有效。比如:

insert into @colors (id, value) values (1, 'Green'),(17,'Yellow'),(33,'Red'),(47,'Black)
insert into @people (id, name) values (1, 'John'),(2,'Paul'),(3,'George'),(7,'Ringo'),(15,'Mick'),(16,'Keith'),(37,'Ronnie')
无论颜色表中有多少行,它都可以工作。使用上面的示例输出,ID范围中有间隙:

+----+--------+----+--------+
| id | value  | id |  name  |
+----+--------+----+--------+
|  1 | Green  |  1 | John   |
| 17 | Yellow |  2 | Paul   |
| 33 | Red    |  3 | George |
| 47 | Black  |  7 | Ringo  |
|  1 | Green  | 15 | Mick   |
| 17 | Yellow | 16 | Keith  |
| 33 | Red    | 37 | Ronnie |
+----+--------+----+--------+

谢谢你,回答得很好;如果第一个表中有3行,第二个表中有7行怎么办?@thx0125:如果第一个表中有3行,第二个表中有7行,那么使用%3而不是%2。第二个表中的前6行将按预期与第一个表匹配。第二个表格的第七行将与第一个表格的第一行匹配,这样第一个表格将出现3次-第一行,第一个表格的第二行和第三行仅出现2次;如果第一个表中有3行,第二个表中有7行怎么办?@thx0125:如果第一个表中有3行,第二个表中有7行,那么使用%3而不是%2。第二个表中的前6行将按预期与第一个表匹配。第二个表格中的第7行将与第一个表格的第一行匹配,这样第一个表格-第一行将出现3次,第一个表的第二行和第三行只出现了两次。我将每个示例表中的行数更改为奇数。我将每个示例表中的行数更改为奇数