选择在预期的此位置无效(

时间:2018-12-26 11:28:37

标签: mysql sql-server

我已经用CASE编写了一个查询,但遇到了()问题。

select SM.subscriber_name as name ,
       SM.accountType as accountType,
       SM.middlename as middleName,
       SM.lastname as lastName,
       SM.title as title,
       SM.email as email,
       SM.company as company,
       SM.email1 as aEmail,
       ,
       SM.zipcode as zipcode,
       SM.phone_no as phoneNumber,
       SM.landlinenumber as landlineNumber,
       SM.login_id as loginId, 
       SD.subscriberType as subscriptionType,
       SD.product_id as productType,
       case SM.state when 'null' then '' as state else STDD.state_name as state end,
       case SM.city when 'null' then '' as city else  CDD.city_name as city end,
       case SM.country when 'null' then '' as country else CD.country_name as country end,
       SD.fulldownloadaccess as fullDownloadAccess,
       SD.emailid_limit as emailLimit,
       SD.acessTime as planTime
from subscriber_master SM , 
     subsciber_details SD,
     city_details CDD,
     state_details STDD, 
     country_details CD
where SM.subscriber_id=16704 and 
      SM.subscriber_id=SD.subscriber_id and 
      SM.country = CD.country_id and
      SM.state = STDD.state_id and
      SM.city = CDD.city_id;

请帮助我在哪里放置括号

3 个答案:

答案 0 :(得分:1)

您的查询有两个问题。首先,您的CASE表达式书写不正确,例如,在任何AS部分(请参阅manual)之前,表达式必须完整。

case SM.state when 'null' then '' as state else STDD.state_name as state end,

应写为

case SM.state when 'null' then '' else STDD.state_name end as state,

另外,如果要检查NULL值而不是字符串值'null',则需要将CASE表达式写为:

case when SM.state IS NULL then '' else STDD.state_name end as state

在途中,您还有一个额外的,(在SM.email1 as aEmail,SM.zipcode as zipcode,之间)。

但是这些问题都不会在标题中给您消息,您是否有未向我们展示的代码?

答案 1 :(得分:0)

我想说您有兴趣将NULL显示为空值,因此,您需要像以下那样重建查询:

  CASE sm.state 
          WHEN 'null' THEN ''  AS state 
          ELSE stdd.state_name AS state 
                                  end, 

要改写为:

  CASE  
          WHEN sm.state IS NULL THEN ''  
          ELSE stdd.state_name END AS state, 

对于所有“ NULL”都相同。另外,AS应该写在CASE结束之后

评论:刚刚意识到下面的句子应该返回相同的句子,但是更加紧凑并且易于阅读:

ISNULL(sm.state, '') AS state,

答案 2 :(得分:0)

您可以如下所示更改case语句,它将同时检查null和空白值

case ISNULL(SM.[state],'') when '' then '' else STDD.state_name end as [state],
       case ISNULL(SM.city,'') when '' then '' else CDD.city_name end as city,
       case ISNULL(SM.country,'') when '' then '' else CD.country_name end as country

您应该使用join在select语句中提供不同表之间的where条件。另外,您也可以使用以下修改后的查询

select SM.subscriber_name as name ,
       SM.accountType as accountType,
       SM.middlename as middleName,
       SM.lastname as lastName,
       SM.title as title,
       SM.email as email,
       SM.company as company,
       SM.email1 as aEmail,       
       SM.zipcode as zipcode,
       SM.phone_no as phoneNumber,
       SM.landlinenumber as landlineNumber,
       SM.login_id as loginId, 
       SD.subscriberType as subscriptionType,
       SD.product_id as productType,
       case ISNULL(SM.[state],'') when '' then '' else STDD.state_name end as [state],
       case ISNULL(SM.city,'') when '' then '' else CDD.city_name end as city,
       case ISNULL(SM.country,'') when '' then '' else CD.country_name end as country,
       SD.fulldownloadaccess as fullDownloadAccess,
       SD.emailid_limit as emailLimit,
       SD.acessTime as planTime
from subscriber_master SM, 
     subsciber_details SD,
     city_details CDD,
     state_details STDD, 
     country_details CD      
where SM.subscriber_id=16704
 and  SM.subscriber_id=SD.subscriber_id and 
      SM.country = CD.country_id and
      SM.[state] = STDD.state_id and
      SM.city = CDD.city_id;