如何制作这个sql。 (条件sql)

时间:2010-09-30 04:18:43

标签: sql mysql

查询:

  SELECT * 
    FROM Post 
ORDER BY Post.rating <-- Here , i want ( Post.rating * 5 ) when Post.recommended = 1 ) 

感谢所有人。

5 个答案:

答案 0 :(得分:4)

使用:

  SELECT p.*
    FROM POST p
ORDER BY CASE 
           WHEN p.recommended = 1 THEN p.rating * 5
           ELSE p.rating
         END

答案 1 :(得分:1)

SELECT * FROM Post 
ORDER BY case Post.rating when 1 then post.rating * 5 else post.rating end

答案 2 :(得分:1)

这有用吗?

SELECT *
  FROM Post
 ORDER BY CASE Recommended WHEN 1 THEN Rating * 5 ELSE Rating END

由于只有一个表,因此列引用是明确的,因此我删除了Post.前缀,这避免了水平滚动条。

答案 3 :(得分:1)

  SELECT * 
    FROM Post 
ORDER BY Post.rating * IF(recommended = 1, 5, 1)

答案 4 :(得分:0)

SELECT * 
    FROM Post 
ORDER BY if(Post.recommended = 1,Post.rating * 5,Post.rating)