zoukankan      html  css  js  c++  java
  • MySQL笔记11

    介绍MYSQL中的逻辑表达式。

    1.IF表达式

    '''
    IF([your conditions],[value outputted if conditions are met],[value outputted if conditions are NOT met])
    '''
    SELECT created_at, IF(created_at<'2014-06-01','early_user','late_user') AS user_type
    FROM users

    嵌入IF(nested IF ):

    '''
     We could use a nested query to say whenever "country" does not equal "US", use the results of a second IF expression to determine whether the outputed value should be "Not Applicable" or "Outside US."
    The IF expression would look like this
    ''' IF(cleaned_users.country='US','In US', IF(cleaned_users.country='N/A','Not Applicable','Outside US'))
    %%sql
    SELECT IF(cleaned_users.country='US', 'In US',
             IF(cleaned_users.country='N/A','Not Applicable', 'Outside US')) AS US_user,
            COUNT(cleaned_users.user_guid)
    FROM (SELECT DISTINCT user_guid, country
         FROM users
         WHERE country IS NOT NULL) AS cleaned_users
    GROUP BY US_user

    2.CASE 语句

    %%sql
    SELECT CASE WHEN cleaned_users.country="US" THEN "In US"
                WHEN cleaned_users.country="N/A" THEN "Not Applicable"
                ELSE "Outside US"
                END AS US_user, 
                COUNT(cleaned_users.user_guid)
    FROM (SELECT DISTINCT user_guid, country            
        FROM users
        WHERE country IS NOT NULL) AS cleaned_users
    GROUP BY US_user

    或者可以写成如下形式:

    '''
    CASE_Value
    '''
    %%sql
    SELECT CASE cleaned_users.country
                WHEN "US" THEN "In US"
                WHEN "N/A" THEN "Not Applicable"
                ELSE "Outside US"
                END AS US_user, COUNT(cleaned_users.user_guid)
    FROM (SELECT DISTINCT user_guid, country   
        FROM users
        WHERE country IS NOT NULL) AS cleaned_users
    GROUP BY US_user
    %%sql 
    SELECT d.dog_guid, d.weight, CASE 
            WHEN d.weight>0 AND d.weight <= 10 THEN "very small"
            WHEN d.weight>10 AND d.weight <= 30 THEN "small"
            WHEN d.weight>30 AND d.weight <= 50 THEN "medium"
            WHEN d.weight>50 AND d.weight <= 85 THEN "large"
            WHEN  d.weight>85 THEN "very large"
            END AS Dog_size
    FROM  dogs d
    LIMIT 200;

    3. 逻辑表达式

    NOT, AND, OR

    '''
    CASE WHEN "condition 1" OR "condition 2" AND "condition 3"
    CASE WHEN "condition 3" AND "condition 1" OR "condition 2"
    CASE WHEN ("condition 1" OR "condition 2") AND "condition 3"
    '''
    
    SELECT COUNT(DISTINCT dog_guid),
            CASE WHEN breed_group='Sporting' OR breed_group='Herding' AND exclude!='1' THEN "group 1"
            ELSE "everything else"
            END AS groups
    FROM dogs         
    GROUP BY groups
    The Safest Way to Get what you Want is to Try and Deserve What you Want.
  • 相关阅读:
    [A类会议] 国内论文检索
    [NISPA类会议] 怎样才能在NIPS 上面发论文?
    [国际A类会议] 2018最最最顶级的人工智能国际峰会汇总!CCF推荐!
    [DEFCON全球黑客大会] 针对CTF,大家都是怎么训练的?
    [DEFCON全球黑客大会] CTF(Capture The Flag)
    推荐系统之--- 评分预测问题
    推荐系统之--- 推荐系统实例
    推荐系统学习 -- 利用社交网络数据
    推荐系统学习 -- 利用上下文信息
    推荐系统学习 -- 利用用户标签数据
  • 原文地址:https://www.cnblogs.com/Shinered/p/9686884.html
Copyright © 2011-2022 走看看