sqlite 如何选择给定的case语句作为列

7uzetpgm  于 2022-12-29  发布在  SQLite
关注(0)|答案(1)|浏览(135)

我想在给出case语句后查询表/

%sql SELECT user_id, user_name,\
       email,\
       CASE WHEN user_name = 'ruo' THEN 'No'\
            WHEN user_name = 'ruoman' THEN 'Yes' \
            else 'Other' END AS New_Data FROM Pes

%sql从Pes中选择user_name,其中New_Data =“Yes”--〉返回错误-〉没有此类列

tjvv9vkg

tjvv9vkg1#

WHERE子句中使用CASE表达式:

SELECT user_name
FROM Pes
WHERE CASE user_name 
        WHEN 'ruo' THEN 'No'
        WHEN 'ruoman' THEN 'Yes' 
        ELSE 'Other' 
      END = 'Yes';

或者,首先选择CASE表达式,然后使用SQLite的特性允许WHERE子句中的派生列:

SELECT user_name,
       CASE user_name 
         WHEN 'ruo' THEN 'No'
         WHEN 'ruoman' THEN 'Yes' 
         ELSE 'Other' 
       END AS New_Data 
FROM Pes
WHERE New_Data = 'Yes';

相关问题