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 相关子查询
基础教程推荐
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 在多列上分布任意行 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- oracle区分大小写的原因? 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
