MySQL select where column is not empty

后端 未结 13 2125
深忆病人
深忆病人 2020-11-30 18:18

In MySQL, can I select columns only where something exists?

For example, I have the following query:

select phone, phone2
from jewishyellow.users
wh         


        
相关标签:
13条回答
  • 2020-11-30 18:35

    We can use CASE for setting blank value to some char or String. I am using NA as Default string.

    SELECT phone,   
    CASE WHEN phone2 = '' THEN 'NA' END AS phone2 ELSE ISNULL(phone2,0) 
    FROM jewishyellow.users  WHERE phone LIKE '813%'
    
    0 讨论(0)
  • 2020-11-30 18:41
    select phone, phone2 from jewishyellow.users 
    where phone like '813%' and phone2 is not null
    
    0 讨论(0)
  • 2020-11-30 18:43

    Compare value of phone2 with empty string:

    select phone, phone2 
    from jewishyellow.users 
    where phone like '813%' and phone2<>''
    

    Note that NULL value is interpreted as false.

    0 讨论(0)
  • 2020-11-30 18:45

    Another alternative is to look specifically at the CHAR_LENGTH of the column values. (not to be confused with LENGTH)

    Using a criteria where the character length is greater than 0, will avoid false positives when the column values can be falsey, such as in the event of an integer column with a value of 0 or NULL. Behaving more consistently across varying data-types.

    Which results in any value that is at least 1 character long, or otherwise not empty.

    Example https://www.db-fiddle.com/f/iQvEhY1SH6wfruAvnmWdj5/1

    SELECT phone, phone2
    FROM users
    WHERE phone LIKE '813%'
    AND CHAR_LENGTH(phone2) > 0
    

    Table Data

    users
    phone (varchar 12) | phone2 (int 10)
    "813-123-4567"     | NULL
    "813-123-4567"     | 1
    "813-123-4567"     | 0
    
    users2
    phone (varchar 12) | phone2 (varchar 12)
    "813-123-4567"     | NULL
    "813-123-4567"     | "1"
    "813-123-4567"     | "0"
    "813-123-4567"     | ""
    

    CHAR_LENGTH(phone2) > 0 Results (same)

    users
    813-123-4567       | 1
    813-123-4567       | 0
    
    users2
    813-123-4567       | 1
    813-123-4567       | 0
    

    Alternatives

    phone2 <> '' Results (different)

    users
    813-123-4567       | 1
    
    users2
    813-123-4567       | 1
    813-123-4567       | 0
    

    phone2 > '' Results (different)

    users
    813-123-4567       | 1
    
    users2
    813-123-4567       | 1
    813-123-4567       | 0
    

    COALESCE(phone2, '') <> '' Results (same)
    Note: the results differ from phone2 IS NOT NULL AND phone2 <> '' which is not expected

    users
    813-123-4567       | 1
    813-123-4567       | 0
    
    users2
    813-123-4567       | 1
    813-123-4567       | 0
    

    phone2 IS NOT NULL AND phone2 <> '' Results (different)

    users
    813-123-4567       | 1
    
    users2
    813-123-4567       | 1
    813-123-4567       | 0
    
    0 讨论(0)
  • 2020-11-30 18:54

    To check if field is NULL use IS NULL, IS NOT NULL operators.

    MySql reference http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html

    0 讨论(0)
  • 2020-11-30 18:54

    If there are spaces in the phone2 field from inadvertant data entry, you can ignore those records with the IFNULL and TRIM functions:

    SELECT phone, phone2
    FROM jewishyellow.users
    WHERE phone LIKE '813%'
        AND TRIM(IFNULL(phone2,'')) <> '';
    
    0 讨论(0)
提交回复
热议问题