简体   繁体   English

如何解决mySql SELECT中的歧义错误1052?

[英]How to solve the ambiguity error 1052 in mySql SELECT?

I'm making a mySql SELECT:我正在制作一个 mySql SELECT:

SELECT name, description, brand, Providers.name, Categories.name, sellingPrice, quantity
FROM Products 
INNER JOIN Providers ON Products.idProvider = Providers.id
INNER JOIN Categories ON Products.idCategory = Categories.id
WHERE category = 'tools';

but I become this error:但我变成了这个错误:

Error Code: 1052. Column 'name' in field list is ambiguous

even if I specified 'Table.column' (Providers.name, Categories.name).即使我指定了“Table.column”(Providers.name,Categories.name)。

Some help please ??请一些帮助?

Did you try removing the name field in the beginning?您是否尝试在开始时删除名称字段? Try this code试试这个代码

SELECT description, brand, Providers.name, Categories.name, sellingPrice, quantity
FROM Products 
INNER JOIN Providers ON Products.idProvider = Providers.id
INNER JOIN Categories ON Products.idCategory = Categories.id
WHERE category = 'tools';

Similarly provide proper Table.Column mapping in the select.同样在选择中提供适当的 Table.Column 映射。 If you have a name field in Products table, include Products.name in select如果产品表中有名称字段,请在选择中包含 Products.name

Whenever you join tables , you may find same column names in multiple tables.每当您连接表时,您可能会在多个表中发现相同的列名。 The SQL Engine doesnt know which to pick.So in order to differentiate you need to specify proper mappings. SQL 引擎不知道选择哪个。所以为了区分你需要指定正确的映射。 You can also specify simple Alias names instead of full Table names.您还可以指定简单的别名而不是完整的表名称。

Simple rule: Qualify all column names when you write a query.简单规则:在编写查询时限定所有列名。 Always.总是。

If you give tables reasonable aliases, this is easy.如果您给表提供合理的别名,这很容易。 So your query should look something like this:所以你的查询应该是这样的:

SELECT p.name, p.description, p.brand, pr.name, c.name, p.sellingPrice, p.quantity
FROM Products p JOIN
     Providers pr
     ON p.idProvider = pr.id JOIN
     Categories c
     ON p.idCategory = c.id
WHERE c.category = 'tools';

I am guessing what tables the columns are coming from, so the qualified names may not be correct (your question doesn't provide this information).我猜测列来自哪些表,因此限定名称可能不正确(您的问题未提供此信息)。

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

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