简体   繁体   English

SQL Server 2008,通过从不共享相同列信息的两个不同表中选择两个列来获取新表

[英]SQL server 2008, get a new table by selecting two columns from two different tables that do not share same column information

this question is based on 这个问题是基于

SQL 2008 R2 CTE syntax error in a SELECT statement SELECT语句中的SQL 2008 R2 CTE语法错误

On SQL server 2008, I need to get a new table by selecting two columns from two different tables. 在SQL Server 2008上,我需要通过从两个不同的表中选择两个列来获取一个新表。

  address (from another_table)    id_num (from a cte created by me)
  city_1                        65 
  city_1                        36
  city_2                        65
  city_2                        36
  city_3                        65
  city_3                        36

Suppose that id_num has only 65 and 36 two values. 假设id_num只有65和36两个值。 The cte has no column of "address". CTE没有“地址”列。 the another_table has no column of id_num. another_table没有id_num列。

for each address, I need to associate all id_num in cte to the address 对于每个地址,我需要将cte中的所有id_num关联到该地址

Any help would be appreciated. 任何帮助,将不胜感激。

If this isn't a join, and you want to associate all id_nums to all addresses, then use: 如果这不是联接,并且您要将所有id_nums与所有地址相关联,请使用:

select distinct address, id_num
from another_table, cte

If you want the id_num to only be associated with addresses that match some criteria add a where clause: 如果希望将id_num仅与符合某些条件的地址相关联,请添加where子句:

where cte.field1 = address.field1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据SQL Server中一个表中列的值从两个表中获取一个新表: - get a new table from two tables based on the columns' values in a table in SQL server: 使用SQL Server 2008,如何连接具有相同列名但具有不同数据类型的两个表 - Using SQL Server 2008, how to join two tables with same column names, but different datatypes 两个共有2列的SQL表需要在表A中但不在表B中的记录(列1相同,但列2不同) - two SQL tables with 2 columns in common, need records that are in table A but not in table B (column 1 is same in both but different column 2) 我如何连接两个表,其中两列引用 SQL 服务器中第二个表中的同一列 - How can i join two tables with two columns that refers the same column in the second table in SQL Server SQL Server 2008比较同一数据库中的两个表,并更改get列 - SQL Server 2008 compare two tables in same database and get column is changed 从一个表中的两个不同列中选择数据,这些列指向另一个表中的同一列 - selecting data from two different columns in one table that point to the same column in another table SQL-将两个表中的两个列相加并写入新列 - SQL - SUM two columns from two tables and write in new Column SQL(Server 2008)从不同的表中检索(不同的)数据(存储在新表的列中) - SQL (server 2008) retrieve (distinct) data from different tables (stored in a new table's column) 通过从两列中选择值来插入同一表中的一列 - insert by selecting values from two columns into a column from the same table 从两个表中选择数据,然后将其插入新表? (SQL / PHP) - Selecting data from two tables, inserting it into new table? (SQL / PHP)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM