在一个 mysql 查询中获取男性/女性/所有人的平均投票评级
我有以下头痛......我试图一次获得三个不同子句的平均评级,我想也许使用 UNION 命令会起作用,但它只会从第一个查询(要求平均评级的查询)中找到值。 averageMaleRating 和averageFemaleRating 作为未知列返回...
这是查询!
SELECT
averageRating,
averageMaleRating,
averageFemaleRating
FROM (
SELECT
question_id AS q_id,
ROUND(AVG(rating)) AS averageRating
FROM
wp_ratings
WHERE
club_id = ?
GROUP BY question_id
UNION
SELECT
question_id AS q_id,
ROUND(AVG(rating)) AS averageMaleRating
FROM
wp_ratings
WHERE
club_id = ?
AND
GENDER = 'male'
GROUP BY question_id
UNION
SELECT
question_id AS q_id,
ROUND(AVG(rating)) AS averageFemaleRating
FROM
wp_ratings
WHERE
club_id = ?
AND
GENDER = 'female'
GROUP BY question_id
)AS bigU
JOIN
wp_ratings
ON
wp_ratings.question_id = bigU.q_id
I have the following headache… I'm trying to get the average rating for three different clauses in one go, I thought maybe using the UNION command would work but it will only find values from the first query (the one asking for averageRating). averageMaleRating and averageFemaleRating are returned as unknown columns…
And here's the query!
SELECT
averageRating,
averageMaleRating,
averageFemaleRating
FROM (
SELECT
question_id AS q_id,
ROUND(AVG(rating)) AS averageRating
FROM
wp_ratings
WHERE
club_id = ?
GROUP BY question_id
UNION
SELECT
question_id AS q_id,
ROUND(AVG(rating)) AS averageMaleRating
FROM
wp_ratings
WHERE
club_id = ?
AND
GENDER = 'male'
GROUP BY question_id
UNION
SELECT
question_id AS q_id,
ROUND(AVG(rating)) AS averageFemaleRating
FROM
wp_ratings
WHERE
club_id = ?
AND
GENDER = 'female'
GROUP BY question_id
)AS bigU
JOIN
wp_ratings
ON
wp_ratings.question_id = bigU.q_id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用 UNION 语句完全错误。
您需要从问题表中选择并加入每个性别的评分表
You're using UNION statement totally wrong.
You need to select from Question table and join Ratings table for each gender