问题
trying to write a mysql query and having a lot of difficult with this one.
I have two tables( Item: info about items, and itemReview: reviews for the items )
What I would like to do is select all the items that belong to a particular location (which is what my outer query does) and then for each item in the outer query, get the average of all the rating fields in the itemReview table
Here is my attempt:
SELECT
Item.idDish,
Item.dateAdded,
Item.dateModified,
Item.fidLocation,
Item.category,
Item.description,
Item.price,
Item.name,
Item.fullImageName,
Item.thumbnailImageName,
sub.rating
FROM Item
JOIN (
SELECT
AVG(ItemReview.rating) AS rating
FROM ItemReview
WHERE ItemReview.fidItem = Item.idItem
) AS sub
WHERE Item.fidLocation = '63';
but mySQL says: Unknown column 'Item.idItem' in 'where clause'
Any help would be very appreciated!! thanks!!
回答1:
You are trying to access the Item.idItem
inside of the subquery but it is not available there. You should use something like this:
SELECT
Item.idDish,
Item.dateAdded,
Item.dateModified,
Item.fidLocation,
Item.category,
Item.description,
Item.price,
Item.name,
Item.fullImageName,
Item.thumbnailImageName,
sub.rating
FROM Item
JOIN
(
SELECT fidItem, AVG(ItemReview.rating) AS rating
FROM ItemReview
GROUP BY ItemReview.fidItem
) AS sub
ON sub.fidItem = Item.idItem
WHERE Item.fidLocation = '63';
回答2:
Query:
SELECT
Item.idDish,
Item.dateAdded,
Item.dateModified,
Item.fidLocation,
Item.category,
Item.description,
Item.price,
Item.name,
Item.fullImageName,
Item.thumbnailImageName,
( SELECT
AVG(ItemReview.rating) AS rating
FROM ItemReview
WHERE ItemReview.fidItem = Item.idItem
) AS rating
FROM Item
WHERE Item.fidLocation = '63'
回答3:
You cannot refer to "outer" tables in such way. One solution is already proposed by bluefeet, another which will also work in your case :
SELECT
Item.idDish,
Item.dateAdded,
Item.dateModified,
Item.fidLocation,
Item.category,
Item.description,
Item.price,
Item.name,
Item.fullImageName,
Item.thumbnailImageName,
(SELECT AVG(ItemReview.rating) AS rating
FROM ItemReview
WHERE ItemReview.fidItem = Item.idItem
) as rating
FROM Item
WHERE Item.fidLocation = '63';
回答4:
Try this
SELECT
Item.idDish,
Item.dateAdded,
Item.dateModified,
Item.fidLocation,
Item.category,
Item.description,
Item.price,
Item.name,
Item.fullImageName,
Item.thumbnailImageName,
sub.rating,
Item.idItem
FROM Item
JOIN (
SELECT
AVG(ItemReview.rating) AS rating ,
fidItem
FROM ItemReview
GROUP BY fidItem
) AS sub on sub.fidItem = Item.idItem
WHERE Item.fidLocation = '63';
回答5:
Try this:
SELECT i.idDish, i.dateAdded, i.dateModified, i.fidLocation, i.category,
i.description, i.price, i.name, i.fullImageName,
i.thumbnailImageName, ir.rating
FROM Item i
LEFT JOIN (SELECT fiditem, AVG(ItemReview.rating) AS rating
FROM ItemReview
GROUP BY fiditem) ir ON ir.fidItem = i.idItem
WHERE i.fidLocation = '63';
回答6:
What about this:
SELECT
Item.idDish,
Item.dateAdded,
Item.dateModified,
Item.fidLocation,
Item.category,
Item.description,
Item.price,
Item.name,
Item.fullImageName,
Item.thumbnailImageName,
sub.rating
FROM Item myTable
JOIN (
SELECT
AVG(ItemReview.rating) AS rating
FROM ItemReview
WHERE ItemReview.fidItem = myTable.idItem
) AS sub
WHERE Item.fidLocation = '63';
Here you are giving a name to main query and referencing the new table name in the subquery. Using the original table names causes MySQL don't know about what table take.
来源:https://stackoverflow.com/questions/14163462/mysql-correlated-subquery