select在此位置无效(

bvk5enib  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(317)

我已经用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;

请帮我把支架放在哪里

koaltpgm

koaltpgm1#

我想说的是,您希望将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
复习:刚刚意识到下面的句子应该是相同的,但要简洁得多,更容易阅读:

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

iqjalb3h2#

你可以改变主意 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;
vuv7lop3

vuv7lop33#

你的查询有几个问题。首先是你的 CASE 如果表达式编写不正确,则表达式必须完整才能执行任何操作 AS 例如,

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, ).
然而,这些问题都不会给你的消息在你的标题,有一些代码你没有给我们看?

相关问题