在SQL Server中,如何联接具有不同列名和不同数据行的两个表?

时间:2022-10-26 07:56:09

Suppose I have two tables T1 & T2. I want resulting output table as O1 with help of a SQL query

假设有两个表T1和T2。在SQL查询的帮助下,我希望输出表为O1

T1 {SName} 
T2 {VName} 

O1 {SName, VName}

6 个解决方案

#1


0  

It seems like you want a cross join of the two tables to include all combinations of rows in T1 and T2. Try this:

似乎您希望两个表的交叉连接包含T1和T2中的所有行组合。试试这个:

Select SName, VName From T1 Inner Join T2 On 1=1

The number of rows you will get is the product of the number of rows in T1 and T2 each.

得到的行数是T1和T2中的行数的乘积。

#2


0  

if you're not joining on anything and want a table of all possible combinations:

如果你不加入任何和想要一个表的所有可能的组合:

select SName, VName
into O1
from T1 cross join T2

if you have a column to join on:

如果你有专栏要加入:

select SName, VName
into O1
from T1 inner join T2 on T1.col = T2.col

#3


0  

Select record from T1 and T2 based on filtering criteria and then insert record in table O1 , use below query to create table O1 and inserting those records.

根据过滤条件从T1和T2中选择record,然后在表O1中插入记录,使用下面的query创建表O1并插入这些记录。

 INSERT INTO O1(SNAME, VNAME) 
    SELECT(T1.SNAME, T2.VNAME)
    From T1 Inner Join T2 On 1=1 //i.e WHERE T1.id=T2.T1_id

use WHERE to filter records

使用WHERE过滤记录

#4


0  

there are several ways of doing it.one easy way is:

有几种方法可以做到这一点。一个简单的方法是:

select T1.SName,T2.VName from T1,T2;

#5


0  

i don't know if i am right, you can use cross join. if you have two tables Players and GameScores then SELECT* INTO O1 FROM GameScores CROSS JOIN Players will return all records where each row from the first table is combined with each row from the second table. Which also mean CROSS JOIN returns the Cartesian product of the sets of rows from the joined tables.

我不知道我说的对不对,你可以用cross join。如果您有两个表,那么玩家和玩家从游戏玩家跨连接玩家中选择*到O1将返回所有记录,其中第一个表中的每一行与第二个表中的每一行结合。这也意味着交叉连接返回来自已连接表的行集合的笛卡尔积。

#6


0  

to get the above result as

得到上述结果

T1 {SName} 
T2 {VName} 
O1 {SName, VName}



 (SELECT * FROM T1,T2) as O1;

will definitely work if both the table have single value if not then you can select the Particular column like T1.SName,T2.VName

如果两个表都只有一个值,那么您可以选择特定的列,比如T1.SName,T2.VName

#1


0  

It seems like you want a cross join of the two tables to include all combinations of rows in T1 and T2. Try this:

似乎您希望两个表的交叉连接包含T1和T2中的所有行组合。试试这个:

Select SName, VName From T1 Inner Join T2 On 1=1

The number of rows you will get is the product of the number of rows in T1 and T2 each.

得到的行数是T1和T2中的行数的乘积。

#2


0  

if you're not joining on anything and want a table of all possible combinations:

如果你不加入任何和想要一个表的所有可能的组合:

select SName, VName
into O1
from T1 cross join T2

if you have a column to join on:

如果你有专栏要加入:

select SName, VName
into O1
from T1 inner join T2 on T1.col = T2.col

#3


0  

Select record from T1 and T2 based on filtering criteria and then insert record in table O1 , use below query to create table O1 and inserting those records.

根据过滤条件从T1和T2中选择record,然后在表O1中插入记录,使用下面的query创建表O1并插入这些记录。

 INSERT INTO O1(SNAME, VNAME) 
    SELECT(T1.SNAME, T2.VNAME)
    From T1 Inner Join T2 On 1=1 //i.e WHERE T1.id=T2.T1_id

use WHERE to filter records

使用WHERE过滤记录

#4


0  

there are several ways of doing it.one easy way is:

有几种方法可以做到这一点。一个简单的方法是:

select T1.SName,T2.VName from T1,T2;

#5


0  

i don't know if i am right, you can use cross join. if you have two tables Players and GameScores then SELECT* INTO O1 FROM GameScores CROSS JOIN Players will return all records where each row from the first table is combined with each row from the second table. Which also mean CROSS JOIN returns the Cartesian product of the sets of rows from the joined tables.

我不知道我说的对不对,你可以用cross join。如果您有两个表,那么玩家和玩家从游戏玩家跨连接玩家中选择*到O1将返回所有记录,其中第一个表中的每一行与第二个表中的每一行结合。这也意味着交叉连接返回来自已连接表的行集合的笛卡尔积。

#6


0  

to get the above result as

得到上述结果

T1 {SName} 
T2 {VName} 
O1 {SName, VName}



 (SELECT * FROM T1,T2) as O1;

will definitely work if both the table have single value if not then you can select the Particular column like T1.SName,T2.VName

如果两个表都只有一个值,那么您可以选择特定的列,比如T1.SName,T2.VName