如果MySQL中返回的结果为NULL,请选择默认值

时间:2013-11-07 15:08:59

标签: mysql sql coalesce

以下查询运行正常,但我想输入一个选项,如果返回的电子邮件为NULL,那么我输入" example@google.com"或类似的东西。我已经阅读了一些函数,比如COALESCE(EMAIL,"example@google.com"),但我不确定该函数在脚本中的位置。你能指点一下我应该在哪里注入这个功能吗?我是否正朝着正确的方向前进?谢谢你。

SELECT LCASE(LOGIN_NAME) as uniqueid, CONCAT('sha-512:', PASSWD) as password, REPLACE(CONTACT_NAME, '"', '') as name, \
     CONCAT('unit,', \
       MAX(CASE WHEN USER_TYPE = 'custom' THEN  \
          'location_employee'  \
        WHEN (LOGIN_NAME != 'link' AND USER_TYPE = 'owner' AND (UA.PARTY_SITE_ID IS NULL OR NOT EXISTS (SELECT U2.ID FROM CUSTOMER_DATA.USER_ACCESS U2 WHERE U2.USER_ID=U.ID AND (U2.PARTY_SITE_ID IS NULL OR U2.PARTY_SITE_ID = '')) ) )THEN  \
          'master'  \
        ELSE  \
          'location'  \
        END)) AS role, \
     MAX(EMAIL) as email, \
     MAX(PHONE) as phone, \
     MAX(FAX) as fax \
     FROM ORACLE_EXPORTS.SHIP_TO ST, \
     CUSTOMER_DATA.USER U \
     JOIN CUSTOMER_DATA.USER_ACCESS UA ON U.ID = UA.USER_ID \
     WHERE ( \
     (UA.PARTY_SITE_ID IS NULL AND ST.CUSTOMER_ID = UA.CUSTOMER_ID ) \
     OR \
     (ST.PARTY_SITE_ID IS NOT NULL AND ST.PARTY_SITE_ID = UA.PARTY_SITE_ID ) \
     ) \
     AND U.LOGIN_NAME IN ( ? ) \
     GROUP BY LOGIN_NAME

3 个答案:

答案 0 :(得分:12)

使用MySQL的IFNULL函数,

<servlet>
    <description></description>
    <display-name>syncro.SynchronizeServlet</display-name>
    <servlet-name>syncro.SynchronizeServlet</servlet-name>
    <servlet-class>syncro.SynchronizeServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>syncro.SynchronizeServlet</servlet-name>
    <url-pattern>/SynchronizeServlet</url-pattern>
    <url-pattern>/SynServlet</url-pattern>
  </servlet-mapping>

答案 1 :(得分:8)

那么,你试图收到电子邮件的地方?

COALESCE(MAX(EMAIL), 'example@google.com') as email

答案 2 :(得分:1)

SELECT coalesce(field_or_derived_value_that_could_be_null, "new value here"), ...
FROM ...
WHERE ...