简体   繁体   中英

How to solve the ambiguity error 1052 in mySql SELECT?

I'm making a 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).

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. If you have a name field in Products table, include Products.name in select

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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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