简体   繁体   中英

I am having trouble figuring out how to fix the error in my mySQL query

I am getting error #1060 - Duplicate column name 'pizzeria' and I am not sure how to fix it.

The question is : Find the pizzeria serving the cheapest pepperoni pizza. In the case of ties, return all of the cheapest pepperoni pizzerias.

SELECT C.pizzeria 
FROM (SELECT pizzeria FROM Serves WHERE pizza="pepperoni") C
LEFT JOIN
(SELECT A.pizzeria, A.price, B.pizzeria, B.price
FROM Serves A, Serves B
WHERE A.pizza = "pepperoni" and B.pizza="pepperoni" and A.price>B.price) D
ON C.pizzeria = D.pizzeria
WHERE D.pizzeria is NULL

Try the following query please:

SELECT C.pizzeria 
FROM (SELECT pizzeria FROM Serves WHERE pizza="pepperoni") C
LEFT JOIN
(SELECT A.pizzeria AS apizzeria, A.price, B.pizzeria AS bpizzeria, B.price
FROM Serves A, Serves B
WHERE A.pizza = "pepperoni" and B.pizza="pepperoni" and A.price>B.price) D
ON C.pizzeria = D.apizzeria
WHERE D.pizzeria is NULL;

This line SELECT A.pizzeria, A.price, B.pizzeria, B.price generates an intermediate result set like below:

pizzeria    price   pizzeria    price
 ..          ..       ...        ...

So when you use D.pizzeria it finds it the corresponding column as duplicate . In fact it's trapped into ambiguous situation while choosing D.pizzeria from A.pizzeria and B.pizzeria (which one to choose?). So get rid of this by using column aliasing.

The problem is that under your left join syntax you have a.pizzeria and b.pizzeria listed. That inline view is then being joined to c.pizzeria but the compiler can't tell which of the pizzeria columns from the inline view you are joining on.

I am very curious as to what you are really trying to accomplish because you are having the optimizer do an awful lot of work.

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