我在Postgres中有一个复杂的查询,我正试图在MySQL中进行转换。Postgres查询有三个链式查询。前两个创建两个公共表,最后一个查询对这两个公共表进行连接。查询的简化版本如下所示。有办法在MySQL中连接这两个公共表吗?我需要这个查询来运行5.6、5.7和8.0,所以8.0中CTE的新特性不是一个解决方案。
(Select table1.y_id as year_id,
SUM(table1.total_weight) AS metric_value
from (SELECT student_name,y_id,total_weight from student_metrics where y_id>10 ) table1
group by year_id
order by metric_value DESC
limit by 5
)table2第三个查询应该在table1和table2上连接table1.y_id = table2.year_id.
为了更好地了解每个查询所做的事情:
发布于 2018-11-13 02:48:09
您可以简单地重复table1子查询:
select
table1.*
from
(select student_name,y_id,total_weight from student_metrics where y_id>10) as table1
inner join (
select tbl1.y_id as year_id,
sum(tbl1.total_weight) as metric_value
from
(select student_name,y_id,total_weight from student_metrics where y_id>10 ) as tbl1
group by tbl1.y_id
order by sum(tbl1.total_weight) desc
limit by 5
) as table2 on table1.y_id = table2.year_id;https://stackoverflow.com/questions/53272801
复制相似问题