在同一SQL查询中使用多个CASE ... WHEN语句时会发生什么? [英] What does it happen when using multiple CASE ... WHEN statements in the same SQL query?

查看:1323
本文介绍了在同一SQL查询中使用多个CASE ... WHEN语句时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的上一个问题之后,我有一个SQL查询,如下所示:

After my previous question, I have an SQL query like the following:

SELECT * 
WHERE ... 
ORDER BY 
  CASE user_id WHEN 34 THEN 1 ELSE 2 END, 
  CASE status WHEN 'active' THEN 1 ELSE 2 END

正如你在上面的代码中可以看到的, >两个 CASE ... WHEN 语句。在这种情况下会发生什么?也就是说,如何记录记录?那些加倍的语句会有冲突吗?

As you can see in the above code, there are two CASE ... WHEN statements. What happens in this case? That is, how records will be ordered? May those "doubled" statements conflict?

UPDATE <以使其以此顺序返回记录:(1)记录 user_id = 34 status ='active' ; (2)记录 status ='active'(不含 user_id 条件); (3)所有其他记录。

UPDATE: I would like to make it to return records in this order: (1) records where user_id = 34 and status = 'active'; (2) records where status = 'active' (without user_id conditions); (3) all other records. How can I make that?

推荐答案

首先将显示所有记录,其中user_id = 34具有活动状态,然后记录user_id = 34且状态不是活动,则记录user_id不等于34且状态等于活动的记录,最后记录状态不是活动的所有剩余记录

First will come all records where user_id =34 with "active" status, then records with user_id=34 and status not "active", then records where user_id not equal 34 and status equals "active" , finally all remaining records where status is not "active"

这篇关于在同一SQL查询中使用多个CASE ... WHEN语句时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆