Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.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_Join_Select - Fatal编程技术网

Sql 如何从多个表中查询列?

Sql 如何从多个表中查询列?,sql,sql-server,join,select,Sql,Sql Server,Join,Select,我需要编写一个SELECT查询来报告表中的特定数据。我的问题是,我需要报告的一列被规范化到另一个表中 我需要报告位于表Consultant中的C/SRep。FKConsultantID位于表客户机中 SELECT ClientName, Country, City, State, Province, Zip, [Address 1], [Address 2], InvestorContact, ConsultantID FROM Clients INNER JOIN

我需要编写一个
SELECT
查询来报告表中的特定数据。我的问题是,我需要报告的一列被规范化到另一个表中

我需要报告位于表
Consultant
中的C/SRep。FK
ConsultantID
位于表
客户机中

SELECT 
    ClientName, Country, City, State, Province, Zip, 
    [Address 1], [Address 2], InvestorContact, ConsultantID
FROM
    Clients
INNER JOIN 
    Consultant ON ConsultantID = C/SRep
以上几行是我尝试过的一件事

结果:

Msg 209,第16级,状态1,第3行
不明确的列名“ConsultantID”

味精207,16级,状态1,第3行
列名“C”无效

味精207,16级,状态1,第3行
无效的列名“SRep”

Msg 209,第16级,状态1,第1行
不明确的列名“ConsultantID”

在编写查询时限定所有列引用,您将永远不会遇到此问题。当然,我不知道数据,但我的想法是:

SELECT cl.ClientName, cl.Country, cl.City, cl.State, cl.Province, cl.Zip, cl.[Address 1], cl.[Address 2],
       co.InvestorContact, co.ConsultantID
FROM Clients cl JOIN
     Consultant co
     ON cl.ConsultantID = co.ConsultantID;
我刚刚确定了列的来源。

在编写查询时对所有列引用进行限定,您将永远不会遇到这个问题。当然,我不知道数据,但我的想法是:

SELECT cl.ClientName, cl.Country, cl.City, cl.State, cl.Province, cl.Zip, cl.[Address 1], cl.[Address 2],
       co.InvestorContact, co.ConsultantID
FROM Clients cl JOIN
     Consultant co
     ON cl.ConsultantID = co.ConsultantID;

我刚刚编好了这些列的来源。

您可能还需要像这里这样的一个基本sql类您问的问题表明您缺少使用sql的一些相关知识

您可能还需要像这里这样的一个基本sql类您问的问题表明您缺少一些相关知识使用sql的知识

如何限定所有列引用?谢谢你的回复@杰姆伯恩沃斯。通过包括列来自的表的别名。选择Clients.ClientName,Clients.Country,Clients.City,Clients.State,Clients.Province,Clients.Zip,Clients.[Address 1],Clients.[Address 2],Clients.InvestorContact,Consultant.[C/SRep]from Clients JOIN Consultant ON Clients.consultanttid=Consultant.consultanttid@Sujitmohanty30是的。谢谢你的检查。正在尝试获取有关将数据类型nvarchar转换为numeric now x时出错的帮助。x@DaleK我已经对戈登林诺夫的答案投了赞成票,但不知道该如何接受。我仍在学习这个网站的布局,当我试图找到解决问题的方法时,我感到非常疲惫。我现在已经接受了答案。谢谢您的检查。我如何限定所有列引用?谢谢你的回复@杰姆伯恩沃斯。通过包括列来自的表的别名。选择Clients.ClientName,Clients.Country,Clients.City,Clients.State,Clients.Province,Clients.Zip,Clients.[Address 1],Clients.[Address 2],Clients.InvestorContact,Consultant.[C/SRep]from Clients JOIN Consultant ON Clients.consultanttid=Consultant.consultanttid@Sujitmohanty30是的。谢谢你的检查。正在尝试获取有关将数据类型nvarchar转换为numeric now x时出错的帮助。x@DaleK我已经对戈登林诺夫的答案投了赞成票,但不知道该如何接受。我仍在学习这个网站的布局,当我试图找到解决问题的方法时,我感到非常疲惫。我现在已经接受了答案。谢谢你的检查。