简体   繁体   English

在case的then子句中使用select语句

[英]using select statement in then clause of case

I'm trying to include select statement in the then of case statement but the output is not as expected. 我试图在case语句的then中包含select语句,但是输出不符合预期。 I know there is different method to do this but can it be done the way i'm trying to do. 我知道有不同的方法可以做到这一点,但是可以按照我尝试的方式完成。

Using the following example data: 使用以下示例数据:

create table example(name varchar(10));

insert into example values
('abc'),('bcd'),('xyz');

I have tried this query (here is the fiddle ): 我已经尝试过此查询(这是小提琴 ):

select 
case when ((select * from example where name='abc')>=1)
then (select * from example where name='abc')
else (select count(*) from example)
end
from example

But it outputs 但它输出

3
3
3

Expected output if name='abc' exist 如果name='abc'存在,则预期输出

name
abc

if not the count(*) 如果不是count(*)

Thanks in advance 提前致谢

Your subquery in the example is (select * from example where name='abc') which is a result set, not a scalar value. 示例中的子查询是(select * from example where name='abc')是结果集,而不是标量值。 Currently it "works" because it is comparing the only column in the table to the value 1 but if you had more than one column in the table it would error out. 当前它“起作用”是因为它将表中的唯一列与值1进行比较,但是如果表中有多个列,则会出错。 Perhaps you intended (select count(*) from example where name='abc') ? 也许您打算(select count(*) from example where name='abc')

Similarly, the THEN clause in a case can only be used to provide a single column value. 同样,在某种情况下,THEN子句只能用于提供单个列值。 In order to do this, perhaps you meant the following: 为此,也许您的意思是:

select 
    case when exists (select * from example where name='abc')
              then (select name from example where name='abc')
         else (select count(*) from example)
    end
from example

But even here you will get three rows and there is no correlation between the rows in example and the result set, so I am not really sure what you're trying to do. 但是即使在这里,您也会得到三行,并且example的行与结果集之间没有任何关联,因此我不确定您要做什么。 I imagine there is a higher purpose though so I will leave it at that. 我想有一个更高的目标,所以我将其保留。

This should do the trick 这应该可以解决问题

select distinct
case when ((select count(name) from example where name='abc')>=1)
then (select * from example where name='abc')
else (select count(*) from example)
end
from example

Let me know if it works. 让我知道它是否有效。

Point 1 : 第一点
For the query, you are trying, the from example in the last will cause to loop through all the records and fetch all the records. 对于查询,您正在尝试,最后一个from example将导致遍历所有记录并获取所有记录。 To restrict that, you have to remove that. 为了限制它,您必须删除它。

Point 2 : 第二点

You can't combine multi row select * in a true condition with a single row count(*) in a false condition. 您不能将true条件下的多行select *false条件下的单行count(*)使用。 You should limit to select a single row. 您应该limit为仅select一行。

Example : 范例

select 
  case when ( select count(*) from example where name='abc' ) >= 1
            then ( select * from example where name='abc' limit 1 )
            else ( select count(*) from example )
        end as name

No need to bother with the complex queries. 无需打扰复杂的查询。

SELECT COUNT(*) AS ct 
FROM example 
GROUP BY name = 'abc' 
ORDER BY name = 'abc' DESC 
LIMIT 1;

If you really want to use CASE just for the sake of using it: 如果您真的只是为了使用而使用CASE:

SELECT 
   CASE name 
      WHEN 'abc' THEN 'abc' 
      ELSE 'others' 
   END AS name, COUNT(*) AS ct
FROM example 
GROUP BY name = 'abc' 
ORDER BY name = 'abc' DESC 
LIMIT 1;

Try below query, which will work even you enter a second duplicate row as value 'abc'. 请尝试以下查询,即使您输入第二个重复行作为值“ abc”,该查询也将起作用。 Mostly above suggested queries will not work as you enter this duplicate row while as per your query condition (>=1), there can be multiple rows for name as 'abc'. 当您输入此重复的行时,以上大部分建议的查询将不起作用,而根据您的查询条件(> = 1),可以有多个行的名称为“ abc”。

SELECT 
CASE WHEN b.cnt>=1 
THEN a.name 
ELSE (SELECT COUNT(*) FROM EXAMPLE) 
END 
FROM (SELECT DISTINCT NAME FROM EXAMPLE WHERE NAME='abc') a 
JOIN (SELECT NAME,COUNT(*) AS cnt FROM EXAMPLE WHERE NAME='abc') b 
ON a.name=b.name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM