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
从SQL Server select中添加列_Sql_Sql Server - Fatal编程技术网

从SQL Server select中添加列

从SQL Server select中添加列,sql,sql-server,Sql,Sql Server,为了简化我遇到的问题,我正在尝试选择其他列 在select查询中。以下是我如何解释的: 表名称 code name -------------- T001 John T001 Mary city state zip -------------------- NewJ NJ 0001 NewY NY 0002 表位置 code name -------------- T001 John T001 Mary city

为了简化我遇到的问题,我正在尝试选择其他列
在select查询中。以下是我如何解释的:

名称

code    name
--------------
T001    John
T001    Mary
city    state   zip
--------------------
NewJ     NJ    0001
NewY     NY    0002
位置

code    name
--------------
T001    John
T001    Mary
city    state   zip
--------------------
NewJ     NJ    0001
NewY     NY    0002
我正在尝试选择一个
选项
,该选项将显示以下内容:

code  name  city  state   zip  name2  city2  state2 zip2
---------------------------------------------------------
T001  John  NewJ    NJ   0001   Mary  NewY     NY   0002
我尝试使用:

select
    a.code,
    a.name, a.city, a.state, a.zip,
    b.name as name2,
    b.city as city2,
    b.state as state2,
    b.zip as zip2
from 
    Names a
left join 
    (select name, city, state, zip 
     from Places b) b on b.code = a.code
获取一个错误,说明它找不到代码
b.code

使用SQL Server有更好的方法吗?

试试这个

SELECT
  a.code,
  a.name,
  a.city,
  a.state,
  a.zip,
  b.name as name2,
  b.city as city2,
  b.state as state2,
  b.zip as zip2
FROM
  Names a
  LEFT JOIN Places b on b.zip = a.code

b、 zip=a.code我想这两个表是如何关联的?它可能是由一个ID关联的。你应该将别名更改为
n
p
,这样它们就不是任意字母。我想我需要找到另一种方法来做到这一点,可能是使用嵌套的select语句,允许选择多个列。