简体   繁体   English

内部联接-SQL Server

[英]Inner join - SQL Server

I used Northwind database and test with following query : 我使用了Northwind数据库并使用以下查询进行测试:

SELECT *
  FROM products
  JOIN suppliers ON suppliers.supplierID = products.supplierID

and I got a red message like this : 然后我收到一条红色消息:

Msg 4104, Level 16, State 1, Line 1 讯息4104,第16级,状态1,第1行
The multi-part identifier "products.supplierID" could not be bound. 无法绑定多部分标识符“ products.supplierID”。

Can anyone shed some light ? 谁能阐明一些想法? many thanks, 非常感谢,

SELECT suppliers.supplierID,products.supplierID
  FROM products
  JOIN suppliers ON suppliers.supplierID = products.supplierID

You should have to explicitly say which supplierid you need to display in results..put the columns on conflict (mandatory) on select statement 您必须明确说明要在结果中显示的供应商编号。.在选择语句中将冲突列(强制性)放入

My only guess based on this error and your query is that there is no SupplierId in the Products table. 基于此错误和您的查询,我唯一的猜测是在Products表中没有SupplierId。 I would check the schema first. 我将首先检查架构。

I've just run the nortwind db script and ran the query and it runs fine for me. 我刚刚运行了nortwind db脚本并运行了查询,它对我来说运行良好。

SELECT *
FROM products
INNER JOIN suppliers ON suppliers.supplierID = products.supplierID

Have a look at the products table, is the supplierId there? 看一下产品表,其中有vendorId吗? if so then make sure you are running your query against the right database. 如果是这样,请确保对正确的数据库运行查询。

can you run the below queries and are they running successfully? 您可以运行以下查询,并且运行成功吗? The third query is essentially a join in another way. 第三个查询本质上是另一种方式的联接。

select SupplierID from Suppliers
go
select SupplierID from Products
go
select * from Products p , Suppliers s
where p.SupplierID = s.SupplierID

your code is creating amibiguity in selecting the columns in select list by "select * " it is not getting which table columsn need to be selected as there are two tables used after from clause..do one thing aliase tables and use aliased column names in select list instead of simple * 您的代码正在通过“ select *”选择列表中的列而产生歧义,因为没有从表中选择两个表,因为from子句之后使用了两个表。做一件事别名表并在表中使用别名列名选择列表而不是简单*

eg. 例如。

select a.* from test a inner join xyz b on a.id=b.id

Instead Of SELECT * Write the appropriate columns you want to select from both table 代替SELECT *编写要从两个表中选择的适当列

like SELECT suppliers.supplierID,suppliers.name..etc SELECT suppliers.supplierID,suppliers.name..etc

both tables contain supplierID that is why the error comes. 两个表都包含了providerID,这就是错误发生的原因。

检查数据库的排序规则设置,如果区分大小写,请检查是否正在使用区分大小写的数据对象名称。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM