gpt4 book ai didi

MySQL:如何使用别名构造新字段?

转载 作者:行者123 更新时间:2023-11-29 20:02:25 24 4
gpt4 key购买 nike

我正在尝试使用两个别名(like_countdislike_count)来构造一个用于订单的新字段(score)目的。下面是我当前的查询,但它不起作用。我在以下查询中收到以下错误:“‘字段列表’中存在未知列‘like_count’”。我在 SO 上寻找过类似的问题,但未能找到解决方案。有人知道如何做吗?

SELECT comment.id, comment.content, 

SUM(if(comment_reaction.type = 'like', 1, 0)) as like_count,
SUM(if(comment_reaction.type = 'dislike', 1, 0)) as dislike_count,

((like_count + 1.9208) / (like_count + dislike_count) -
1.96 * SQRT((like_count * dislike_count) / (like_count + dislike_count) + 0.9604) /
(like_count + dislike_count)) / (1 + 3.8416 / (like_count + dislike_count))
as score,

(SELECT comment_reaction.type FROM comment_reaction
WHERE comment_reaction.person_id = :person_id
AND comment.id = comment_reaction.comment_id) as my_reaction

FROM comment
LEFT JOIN comment_reaction ON comment.id = comment_reaction.comment_id

WHERE comment.topic_id = :topic_id
GROUP BY comment.id
ORDER BY score DESC LIMIT 0, 10

最佳答案

将原始查询包装在派生表中。然后添加新列:

select dt.*,
((like_count + 1.9208) / (like_count + dislike_count) -
1.96 * SQRT((like_count * dislike_count) / (like_count + dislike_count) + 0.9604) /
(like_count + dislike_count)) / (1 + 3.8416 / (like_count + dislike_count))
as score

from
(
SELECT
comment.id, comment.content,

SUM(if(comment_reaction.type = 'like', 1, 0)) as like_count,
SUM(if(comment_reaction.type = 'dislike', 1, 0)) as dislike_count,

(SELECT comment_reaction.type FROM comment_reaction
WHERE comment_reaction.person_id = :person_id
AND comment.id = comment_reaction.comment_id) as my_reaction,

FROM comment
LEFT JOIN comment_reaction ON comment.id = comment_reaction.comment_id

WHERE comment.topic_id = :topic_id

GROUP BY comment.id
) dt
ORDER BY score DESC

关于MySQL:如何使用别名构造新字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40483309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com