sql 语句多层嵌套查询 使用别名 字段无效,如何解决(有图)
SELECTtma.idastid,tma.match_idASmatchId,tma.`status`,(SELECTround(avg(s.sumScore),1)F...
SELECT
tma.id as tid,
tma.match_id AS matchId,
tma.`status`,
(SELECT round(avg(s.sumScore),1) FROM (
SELECT
SUM(tri.score) AS sumScore
FROM
`triz_score_info` tri LEFT JOIN triz_review_submit_info trs ON trs.reviewer_id = tri.reviewer_id
WHERE
tri.apply_id = tma.id
GROUP BY
tri.reviewer_id)AS s
) AS avgScore,
tma.pro_name AS proName,
tma.create_time createTime
FROM
triz_match_applyinfo tma
WHERE
tma.match_id = 22 展开
tma.id as tid,
tma.match_id AS matchId,
tma.`status`,
(SELECT round(avg(s.sumScore),1) FROM (
SELECT
SUM(tri.score) AS sumScore
FROM
`triz_score_info` tri LEFT JOIN triz_review_submit_info trs ON trs.reviewer_id = tri.reviewer_id
WHERE
tri.apply_id = tma.id
GROUP BY
tri.reviewer_id)AS s
) AS avgScore,
tma.pro_name AS proName,
tma.create_time createTime
FROM
triz_match_applyinfo tma
WHERE
tma.match_id = 22 展开
3个回答
展开全部
(SELECT round(avg(s.sumScore),1) FROM (
SELECT
SUM(tri.score) AS sumScore
FROM
`triz_score_info` tri LEFT JOIN triz_review_submit_info trs ON trs.reviewer_id = tri.reviewer_id
WHERE
tri.apply_id = tma.id
GROUP BY
tri.reviewer_id)AS s
改为
select round(avg(s.sumScore),1) from ( SELECT
SUM(tri.score) AS sumScore,tri.apply_id
FROM
`triz_score_info` tri LEFT JOIN triz_review_submit_info trs ON trs.reviewer_id = tri.reviewer_id group by tri.reviewer_id,tri.apply_id) aa where aa.apply_id=tma.id
这样应该可以。另外我多句嘴,avg那块我没看懂你怎么平均(一般都是分组直接用啊),或者说是按照什么平均。内层sql求出一个总数,外层直接计算一个平均值,这个平均值是什么?我不知道你的业务结构仅仅是臆测,反正我没看懂(直接照抄了)。
SELECT
SUM(tri.score) AS sumScore
FROM
`triz_score_info` tri LEFT JOIN triz_review_submit_info trs ON trs.reviewer_id = tri.reviewer_id
WHERE
tri.apply_id = tma.id
GROUP BY
tri.reviewer_id)AS s
改为
select round(avg(s.sumScore),1) from ( SELECT
SUM(tri.score) AS sumScore,tri.apply_id
FROM
`triz_score_info` tri LEFT JOIN triz_review_submit_info trs ON trs.reviewer_id = tri.reviewer_id group by tri.reviewer_id,tri.apply_id) aa where aa.apply_id=tma.id
这样应该可以。另外我多句嘴,avg那块我没看懂你怎么平均(一般都是分组直接用啊),或者说是按照什么平均。内层sql求出一个总数,外层直接计算一个平均值,这个平均值是什么?我不知道你的业务结构仅仅是臆测,反正我没看懂(直接照抄了)。
更多追问追答
追问
每个作品(aa.apply_id)有多个老师评分(tri.reviewer_id),每次打分有多个打分项(tri.score),SUM(tri.score)就是当前作品该老师打的总分
我需要查的是所有作品的得分情况(多个老师打的平均分)
追答
根据你的解释,我觉得我的写法上应该能满足要求。第一步求的是每个作品的而每个老师给的一个总分,第二步是每个作品的平均分。应该没什么问题。
不过你的trs表是干什么用的,这里是左连接,而且后面好像也没有用到,不写是不是也可以。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询