1

I made this solution but it continues not returning any value If the user not exists.

select
    case when TypeId = 1 then 1 
         when TypeId = 0 then 2
         when count(1) < 1 then 3
         end 
    from Type 
    where mail = 'name@test.com'
    group by Type
2
  • 2
    The WHERE clause dictates how many rows are returned. Can't your application handle the case where no rows are returned? Count can't be anything but 0 in that case, right? – Aaron Bertrand Jun 24 '16 at 16:41
  • 1
    Which DBMS is this for? – ypercubeᵀᴹ Jun 24 '16 at 19:41
1

Application would be much better place to deal with such cases as Aaron Bertrand said but you can do it with SQL as well .

select
    case when TypeId = 1 then 1 
         when TypeId = 0 then 2
         end 
    from Type 
    where mail = 'name@test.com'
UNION ALL 
SELECT 3 
FROM DUAL 
 WHERE NOT EXISTS (SELECT NULL FROM Type WHERE mail = 'name@test.com')

**Depends on RDMS you're using you may or may not need specify "FROM DUAL".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.