带SELECT的子查询中的CASE

时间:2022-01-19 00:10:22

This is the query that I've written

这是我写的查询

SELECT id, name, (SELECT is_enable FROM customers WHERE id=table_one.id) AS some_tag FROM table_one;

The above query returns the date like this

上面的查询返回这样的日期

    ID NAME                       SOME_TAG
     4 name 1                          0
     3 name 2                          0
     1 name 3                          1

I'm trying to fit in a CASE in the above query so that I get the value of SOME_TAG as "Yes" when 1 and "No" when 0 but no luck so far. Any help/leads will be appreciated! Thanks!

我正在尝试在上面的查询中放入一个CASE,这样我得到SOME_TAG的值为1时为“是”而0时为“否”但到目前为止没有运气。任何帮助/潜在客户将不胜感激!谢谢!

So far I tried this

到目前为止我试过这个

select id, name, (select is_enable case when is_enable is not null then "No" else "Yes" end from customers where id=table_one.id) as some_tag from table_one;

2 个解决方案

#1


3  

You can use a join instead of a sub-query... and then this is how'd the case would work.

您可以使用连接而不是子查询...然后这就是案例的工作方式。

SELECT 
    t.id, 
    t.name, 
    case when c.is_enabled = 1 then 'Yes' else 'No' end 
from 
    table_one t
    left join customers c on
    c.id = t.id 

#2


0  

Below code also works for your problem

下面的代码也适用于您的问题

SELECT 
  id, 
  name, 
  (CASE is_enable 
    WHEN 1 THEN 'YES'
    WHEN 0 THEN 'NO' 
  END) AS TAG
FROM ( SELECT id, name, (SELECT is_enable FROM customers WHERE id=table_one.id) AS some_tag FROM table_one );

#1


3  

You can use a join instead of a sub-query... and then this is how'd the case would work.

您可以使用连接而不是子查询...然后这就是案例的工作方式。

SELECT 
    t.id, 
    t.name, 
    case when c.is_enabled = 1 then 'Yes' else 'No' end 
from 
    table_one t
    left join customers c on
    c.id = t.id 

#2


0  

Below code also works for your problem

下面的代码也适用于您的问题

SELECT 
  id, 
  name, 
  (CASE is_enable 
    WHEN 1 THEN 'YES'
    WHEN 0 THEN 'NO' 
  END) AS TAG
FROM ( SELECT id, name, (SELECT is_enable FROM customers WHERE id=table_one.id) AS some_tag FROM table_one );