mySQL与Subquery相关

时间:2013-01-04 19:18:38

标签: mysql join subquery correlated-subquery

尝试编写一个mysql查询,并且在这个问题上遇到了很多困难。

我有两个表(项目:关于项目的信息,以及项目评论:项目的评论)

我想要做的是选择属于特定位置的所有项目(这是我的外部查询所做的),然后对于外部查询中的每个项目,获取itemReview中所有评级字段的平均值表

这是我的尝试:

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';

但mySQL说:'where子句'中的未知列'Item.idItem'

任何帮助都将非常感谢!!谢谢!

6 个答案:

答案 0 :(得分:7)

您正在尝试访问子查询内部的Item.idItem,但它在那里不可用。你应该使用这样的东西:

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';

答案 1 :(得分:4)

查询:

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'

答案 2 :(得分:0)

试试这个:

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';

答案 3 :(得分:0)

试试这个

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';

答案 4 :(得分:0)

您不能以这种方式引用“外部”表。一个解决方案已经由 bluefeet 提出,另一个也适用于您的情况:

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';

答案 5 :(得分:0)

这个怎么样:

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';

这里给出一个主查询的名称,并在子查询中引用新的表名。使用原始表名会导致MySQL不知道表格占用了什么。