Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个数据库,用于存储等级为 a、b、c 的论坛帖子。我想为 a 级分配 3 分,为 b 级分配 2 分,为 c 级分配 1 分,并获得所有用户的总分。如果重量是偶数,我可以使用这个查询:
select count(*), userId from table_post group by userId;
但是如何在一个查询中计算加权帖子?字段为 post_id、user_id、grade、post_content
SELECT A.USER_ID,SUM(NEW_POINTS) FROM ( SELECT USER_ID, CASE WHEN grade = 'a' THEN points=3 WHEN grade = 'b' THEN points=2 WHEN grade = 'c' THEN points=1 END AS new_points FROM table_post ) A GROUP BY A.USER_ID;