Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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/7/sql-server/21.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 如何从两个列名相同的表中检索数据_Sql_Sql Server - Fatal编程技术网

Sql 如何从两个列名相同的表中检索数据

Sql 如何从两个列名相同的表中检索数据,sql,sql-server,Sql,Sql Server,如何从两个列名相同的表中检索数据?使用别名,即 select table1.field as table1field, table2.field as table2field from table1 join table2 on ... 使用别名 select table1.field as table1field, table2.field as table2field from table1 join table2 on ... 如果需要所有列(非Union),请为其中一个表的每一列

如何从两个列名相同的表中检索数据?

使用别名,即

select table1.field as table1field, table2.field as table2field from table1 
join table2 on ...
使用别名

select table1.field as table1field, table2.field as table2field from table1 
join table2 on ...

如果需要所有列(非Union),请为其中一个表的每一列设置别名。

如果需要所有列(非Union),请为其中一个表的每一列设置别名。

如果要包含两个表中的行,可以使用
Union all

SELECT Col1, Col2, Col3 FROM Table1
UNION ALL
SELECT Col1, Col2, Col3 FROM Table2

如果要包含两个表中的行,可以使用
UNION ALL

SELECT Col1, Col2, Col3 FROM Table1
UNION ALL
SELECT Col1, Col2, Col3 FROM Table2
您可以使用别名

select a.col_name, b.col_name from table1 a, from table2 b
where a.userid =b.userid  // condition as per your comments in question
您可以使用别名

select a.col_name, b.col_name from table1 a, from table2 b
where a.userid =b.userid  // condition as per your comments in question
使用别名??:-)

使用别名??:-)


我必须使用where子句。意味着我需要从usertable中获取userid,并使用它从表1和表2中获取列名相同的数据。两个表都包含userid列。现在我如何应用我所说的:使用别名。您接受的答案甚至与您上面描述的不符。根据问题和OP的评论,所选答案与正确答案完全不同。我需要使用where子句。意味着我需要从usertable中获取userid,并使用它从表1和表2中获取列名相同的数据。两个表都包含userid列。现在我如何应用我所说的:使用别名。你接受的答案甚至与你上面描述的不符。根据问题和OP的评论,选择的答案与正确答案完全不同。下层选民:当我开始写答案时,没有评论解释OP到底想要什么。正如你所看到的,我的回答以“如果…”开头。有趣的是,我得到了反对票,因为OP决定接受我的答案:)+1让你走出困境。我理解。此外,一个被合理接受的答案不应该有一个负数。选民:当我开始写答案时,没有评论解释OP到底想要什么。正如你所看到的,我的回答以“如果…”开头。有趣的是,我得到了反对票,因为OP决定接受我的答案:)+1让你走出困境。我理解。此外,正确接受的答案不应关联负数。