Home > database >  Postgres more than one row returned by a subquery used as an expression
Postgres more than one row returned by a subquery used as an expression

Time:02-01

I don't understand what the problem is, I get this error:

more than one row returned by a subquery used as an expression

select * 
from "History" 
WHERE "Message" LIKE '%' || (select "Name" 
                             from  public."Campaign"
                             where "Employ" IN (SELECT * 
                                                FROM (                                                                 
                                                  SELECT "Employ" 
                                                  FROM public."Sub"
                                                  where  "Company" ilike '%XX%'limit 1
                                                )
                                                union all
                                                (SELECT "Employ" 
                                                 FROM public."Sub"
                                                 where "Company" not ilike '%XX%'limit 1)
                                                ) as t)) || '%' 

CodePudding user response:

Instead of trying to LIKE multiple names, it could LIKE ANY of the names.

select * 
from "History" 
where "Message" LIKE ANY (
     select '%'||"Name"||'%'
     from  public."Campaign"
     where "Employ" IN (
           (select "Employ" from public."Sub" 
            where "Company" ilike '%XX%' limit 1) 
           union all
           (select "Employ" from public."Sub"
            where "Company" not ilike '%XX%' limit 1)
     ) 
);
  •  Tags:  
  • Related