if my query returns empty i want to display input as output

Hi Expets,

query:
=======
select ci_id from category where parent_id =236282
UNION
select ci_id from category where parent_id in(select category_id from
category where parent_id=236282)

the above query some times it will returns empty
i want to display 236282 as the output if my query returns empty

can some one help me how to do it.
LVL 2
srikoteshAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Try this: (not tested)
IF NOT EXISTS (select ci_id from category where parent_id =236282
UNION
select ci_id from category where parent_id in(select category_id from
category where parent_id=236282)
)
BEGIN 
  SELECT 236282
END
ELSE 
BEGIN
(select ci_id from category where parent_id =236282
UNION
select ci_id from category where parent_id in(select category_id from
category where parent_id=236282)
)
END

Open in new window

0
HuaMin ChenProblem resolverCommented:
Try
select isnull(ltrim(ci_id),'236282') from
(select ci_id from category where parent_id =236282
UNION
select ci_id from category where parent_id in(select category_id from
category where parent_id=236282)) a

Open in new window

0
srikoteshAuthor Commented:
hi HuaMinChen,

the above query is not working
0
Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

skullnobrainsCommented:
i assume the category is either itself or it's parent but never both

select
isnull (
  select ci_id from category where parent_id =236282
  ,isnull (
    select ci_id from category where parent_id in(select category_id from
category where parent_id=236282)
    ,236282
  )
)

or maybe

select ci_id from category where parent_id =236282
UNION
select ci_id from category where parent_id in(select category_id from
category where parent_id=236282)
UNION
select 236282 as ci_id
limit 1
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srikoteshAuthor Commented:
I got the solution for this query.

SELECT      * FROM      category WHERE      parent_id = 236282       OR parent_id IN (                   SELECT      category_id                   FROM      category                   WHERE       parent_id = 236282             );
0
skullnobrainsCommented:
which lists all children and grandchildren of the given id (same as the query you posted originally)

not sure about this : "i want to display 236282 as the output if my query returns empty" or what you actually wanted in the first place

good for you if you got it working

ps : you can accept your own answer if you don't want to spend your points
0
srikoteshAuthor Commented:
Hi skullnobrains,

i am getting the expected result from the second query as u posted

THANKS
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.