我想根据以下内容加入两个表

时间:2019-11-06 11:23:56

标签: sql-server

表1

no  name 
1   aaa
2   bbb
3   ccc

table2

date
1/1/19
2/2/19

结果将

no  name  date
1   aaa   1/1/19
2   bbb   1/1/19
3   ccc   1/1/19
1   aaa   2/2/19
2   bbb   2/2/19
3   ccc   2/2/19

2 个答案:

答案 0 :(得分:1)

尝试FULL JOIN

SELECT no, namem, date FROM 
TABLE1 FULL JOIN table2 ON 1 = 1

尝试CROSS JOIN

SELECT no, namem, date  FROM TABLE1 CROSS JOIN table2

DEMO

答案 1 :(得分:1)

SQL Fiddle

MS SQL Server 2017架构设置

create table Table1(num int,name varchar(max))
create table Table2(dates date)
insert into Table1(num,name)values(1,'aaa')
insert into Table1(num,name)values(2,'bbb')
insert into Table1(num,name)values(3,'ccc')
insert into Table2(dates)values('2019-01-01')
insert into Table2(dates)values('2019-02-02')

查询1

SELECT num,name,dates
FROM Table1
CROSS APPLY Table2

Results

| num | name |      dates |
|-----|------|------------|
|   1 |  aaa | 2019-01-01 |
|   2 |  bbb | 2019-01-01 |
|   3 |  ccc | 2019-01-01 |
|   1 |  aaa | 2019-02-02 |
|   2 |  bbb | 2019-02-02 |
|   3 |  ccc | 2019-02-02 |