mySQL correlated Subquery(mySQL 相关子查询)
问题描述
正在尝试编写一个 mysql 查询,但遇到了很多困难.
trying to write a mysql query and having a lot of difficult with this one.
我有两个表(Item:关于项目的信息,以及 itemReview:对项目的评论)
I have two tables( Item: info about items, and itemReview: reviews for the items )
我想做的是选择属于特定位置的所有项目(这是我的外部查询所做的),然后对于外部查询中的每个项目,获取 itemReview 中所有评分字段的平均值表
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
这是我的尝试:
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'
but mySQL says: Unknown column 'Item.idItem' in 'where clause'
任何帮助将不胜感激!谢谢!!
Any help would be very appreciated!! thanks!!
推荐答案
您正在尝试访问子查询中的 Item.idItem,但那里不可用.你应该使用这样的东西:
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';
这篇关于mySQL 相关子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:mySQL 相关子查询
基础教程推荐
- 是否可以执行按位分组功能? 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 无法解决整理冲突 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
