如何在以下 SQL 查询中使用 select 大小写

How to use select Case in the following SQL query

我有两个表 usersuserdetail。我正在尝试创建一个视图,其中如果 userdetail 列的状态为 1,它应该在视图中显示 Active,如果状态为 0:

则被阻止
CREATE VIEW `new` AS 
SELECT 
users.id AS id,
userdetail.healthissues AS healthissues,
users.fullName AS fullname,
userdetail.`status`,

 CASE status
 WHEN  userdetail.status ='1' THEN  userdetail.`status`= 'Blocked'
        WHEN  userdetail.status= '0' THEN  userdetail.`status` ='Active'
    END ,

users.phoneNumber AS number
FROM users
JOIN userdetail ON users.id = userdetail.reference

这段代码没有给出想要的结果。有人可以帮我解决这个问题吗?

我认为这是您想要的正确语法:

CREATE VIEW `new` AS 
    SELECT u.id AS id, ud.healthissues AS healthissues, u.fullName, 
           ud.status as orig_status,
           (CASE WHEN ud.status = 1 THEN 'Blocked'
                 WHEN ud.status = 0 THEN 'Active'
            END) as status,
           u.phoneNumber AS number
    FROM users u JOIN
         userdetail ud
         ON u.id = ud.reference;

备注:

  • 我不确定你是否想要 select 状态和字符串,但你的查询中有它。
  • case 的正确语法不使用 = 然后 then 子句(好吧,除非你想让它成为 return 布尔值)。
  • Table 别名使查询更易于编写和阅读。
  • New 是一个不好的视图名称。虽然不是保留的,但是是关键字。

你不能赋值..只是 select

CREATE VIEW `new` AS 
SELECT 
users.id AS id,
userdetail.healthissues AS healthissues,
users.fullName AS fullname,
userdetail.`status`,
   CASE status
   WHEN  userdetail.status ='1' THEN  'Blocked'
          WHEN  userdetail.status= '0' THEN 'Active'
      END  as my_eval_status,
users.phoneNumber AS number
FROM users
JOIN userdetail ON users.id = userdetail.reference