If else on WHERE clause

150,796

Solution 1

IF is used to select the field, then the LIKE clause is placed after it:

SELECT  `id` ,  `naam` 
FROM  `klanten` 
WHERE IF(`email` != '', `email`, `email2`) LIKE  '%@domain.nl%'

Solution 2

You want to use coalesce():

where coalesce(email, email2) like '%[email protected]%'

If you want to handle empty strings ('') versus NULL, a case works:

where (case when email is NULL or email = '' then email2 else email end) like '%[email protected]%'

And, if you are worried about the string really being just spaces:

where (case when email is NULL or ltrim(email) = '' then email2 else email end) like '%[email protected]%'

As an aside, the sample if statement is really saying "If email starts with a number larger than 0". This is because the comparison is to 0, a number. MySQL implicitly tries to convert the string to a number. So, '[email protected]' would fail, because the string would convert as 0. As would '[email protected]'. But, '[email protected]' and '[email protected]' would succeed.

Solution 3

Note the following is functionally different to Gordon Linoff's answer. His answer assumes that you want to use email2 if email is NULL. Mine assumes you want to use email2 if email is an empty-string. The correct answer will depend on your database (or you could perform a NULL check and an empty-string check - it all depends on what is appropriate for your database design).

SELECT  `id` ,  `naam` 
FROM  `klanten` 
WHERE `email` LIKE  '%[email protected]%'
OR (LENGTH(email) = 0 AND `email2` LIKE  '%[email protected]%')

Solution 4

Here is a sample query for a table having a foreign key relationship to the same table with a query parameter.

enter image description here

SET @x = -1;
SELECT id, categoryName 
FROM Catergory WHERE IF(@x > 0,category_ParentId = @x,category_ParentId IS NOT NULL);

@x can be changed.

Solution 5

try this ,hope it helps

select user_display_image as user_image,
user_display_name as user_name,
invitee_phone,
(
 CASE 
    WHEN invitee_status=1 THEN "attending" 
    WHEN invitee_status=2 THEN "unsure" 
    WHEN invitee_status=3 THEN "declined" 
    WHEN invitee_status=0 THEN "notreviwed" END
) AS  invitee_status
 FROM your_tbl
Share:
150,796

Related videos on Youtube

botenvouwer
Author by

botenvouwer

I'm a GEO/IT consultant in holland

Updated on July 09, 2022

Comments

  • botenvouwer
    botenvouwer almost 2 years

    I've this query:

    SELECT  `id` ,  `naam` 
    FROM  `klanten` 
    WHERE (
    `email` LIKE  '%@domain.nl%'
    OR  `email2` LIKE  '%@domain.nl%'
    )
    

    But I want to do something like this:

    SELECT  `id` ,  `naam` 
    FROM  `klanten` 
    WHERE IF(`email` > 0,
    `email` LIKE  '%@domain.nl%'
    ,  `email2` LIKE  '%@domain.nl%'
    )
    

    How to check if email exist? I want to use email and if this field is empty I want to use email2. How do I accomplish this?

  • Maurício
    Maurício about 11 years
    @mtahmed No it won't - I've noted that in my answer. The question is not clear on what would be the correct check.
  • John Woo
    John Woo about 11 years
    @mtahmed no, COALESCE will only work on NULL. But columns: email, email2 are nullable right?
  • mmtauqir
    mmtauqir about 11 years
    Actually, does email IS '' work instead of LENGTH(email) = 0?
  • botenvouwer
    botenvouwer about 11 years
    this works for me thank you for you answer. Note that I look for the domain and not the email itself. That is why I use LIKE %%.
  • Maurício
    Maurício about 11 years
    @mtahmed Yes, that does work. I was just copying the question as closely as possible, so my answer would be more understandable to the author (although it would be email = '' surely?)
  • botenvouwer
    botenvouwer about 11 years
    The field or the table cell if you like is just '' and not NULL so there you go. ps, the database itself is not made by me!
  • botenvouwer
    botenvouwer about 11 years
    On second tought I like your answer the best
  • mmtauqir
    mmtauqir about 11 years
    The reason I am asking was because when checking for empty string, it's almost always better to do string compare (email = '') instead of checking length (LENGTH(email) = 0) because it's faster to do string compare.
  • Gordon Linoff
    Gordon Linoff about 11 years
    @mtahmed . . . Do you have a reference on why the string compare is faster than the length? Given that the length is stored prior to any data in the string, I would expect a length comparison to possibly be faster. (dev.mysql.com/doc/refman/5.5/en/char.html)
  • mmtauqir
    mmtauqir about 11 years
    Oh I meant it as a general comment: I use string compare whenever I can. Also, it's a one byte compare for string compare while it's 4 or 8 byte compare for length compare (comparing char vs integer).
  • TMH
    TMH almost 10 years
    Can I use IF to filter out more fields? I want to do if (active = 1) AND startDate < UNIX_TIMESTAMP(). Is something like that possible?
  • gen_Eric
    gen_Eric almost 10 years
    @TomHart: IF here is just used to select one field vs the other. It makes no sense in your example. Do you mean: WHERE active=1 AND startDate < UNIX_TIMESTAMP()?
  • TMH
    TMH almost 10 years
    Not exactly, I only want it to check the start date if active is 1.
  • gen_Eric
    gen_Eric almost 10 years
    @TomHart: WHERE active=1 AND startDate < UNIX_TIMESTAMP(). I'm pretty sure MySQL will short circuit and stop evaluating if active isn't 1.
  • TMH
    TMH almost 10 years
    So if I have the startDate criteria at the very end of the WHERE, it should work as intended? (I don't have access to check this at the moment)
  • gen_Eric
    gen_Eric almost 10 years
    @TomHart: I think so. I'm not 100% sure that MySQL does short circuit evaluation. You can also try WHERE IF(active = 1, startDate < UNIX_TIMESTAMP(), 0) (or whatever you want to happen if active is not 1).
  • TMH
    TMH almost 10 years
    I'll look at the IF syntax now, basically if active is 0 (it's only either 1 or 0) it ignores the start date.
  • gen_Eric
    gen_Eric almost 10 years
    @TomHart: Are there more criteria after that? You can also try WHERE CASE WHEN active=1 THEN startDate < UNIX_TIMESTAMP() END. That might be better here.
  • TMH
    TMH almost 10 years
  • TMH
    TMH almost 10 years
    Is that room available on chat.stachexchange? chat.stackoverflow is blocked at my work.
  • gen_Eric
    gen_Eric almost 10 years
    @TomHart: It should be, but I don't have time to join a chat right now.
  • TMH
    TMH almost 10 years
    Okay not a problem, I have a working solution at the moment, but I'll look as using CASE instead as it seems more elegant. Thanks for your help :).
  • menoktaokan
    menoktaokan about 2 years
    This works on HeidiSql but SpringData nativeQuery. Do you know why?
  • gen_Eric
    gen_Eric about 2 years
    @menoktaokan What's the issue you're seeing?
  • menoktaokan
    menoktaokan about 2 years
    @RocketHazmat It was like Spring Data didn't recognize the syntax so I changed the path.