我有一个表User_Engagement有列:-

我试图在更新查询上实现以下公式,以计算单个查询中所有列的TotalInteraction:
totalInteraction = value in totalComments + value in totalLikes * ( SUM of averageLikes / SUM of averageComments)到目前为止,我已经尝试实现这个查询:-
UPDATE User_engagement AS ue
SET
ue.totalInteraction = (ue.totalComments + ue.totalLikes) * (SUM(ue.averageLikes)/SUM(ue.averageComments))上面的查询在MySql中给了我一个错误的MySql。
帮助是非常感谢的。
发布于 2019-06-20 14:10:37
一种办法是:
SELECT @ratio := SUM(averageLikes)/SUM(averageComments)
FROM User_engagement;
UPDATE User_engagement
SET totalInteraction = (totalComments + totalLikes) * @ratio;但这可能不切实际..。当表中有任何变化时会发生什么?您不需要重新计算和更新表中的所有行吗?
相反,在需要时进行计算:
SELECT ue.totalLikes, ue.averageLikes, ue.averageComments,
(ue.totalComments + ue.totalLikes) * x.ratio AS totalInteraction
FROM User_engagement AS ue
JOIN ( SELECT SUM(averageLikes)/SUM(averageComments) as ratio
FROM User_engagement ) AS xhttps://dba.stackexchange.com/questions/241017
复制相似问题