开发者

RE: How make this sql. ( conditions sql )

开发者 https://www.devze.com 2023-01-18 17:05 出处:网络
I questioned here this: How make this sql. ( conditions sql ) but now, I want SELECT SUM( Post.rating ) as countRating <--- THIS i want Post.rating * 5 WHEN Post.recommended = 1

I questioned here this: How make this sql. ( conditions sql )

but now, I want

SELECT SUM( Post.rating ) as countRating <--- THIS i want Post.rating * 5 WHEN Post.recommended = 1
FROM posts as Post
ORDER BY coun开发者_StackOverflowtRating DESC

help me


Basically, use the same technique as in the answers to the other question - use a CASE expression, this time in the SUM instead of in the ORDER BY clause.

SELECT SUM(CASE Recommended WHEN 1 THEN 5 ELSE 1 END * Rating) AS countRating
  FROM posts as Post
 ORDER BY countRating DESC

If the alternative value for Recommended is 0 (a plausible guess), then you could also write:

SELECT SUM((4 * Recommended + 1) * Rating) AS countRating
  FROM posts as Post
 ORDER BY countRating DESC

And, indeed, you could do the same with the ORDER BY clause in the previous question.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号