如何在SQL Server中使用此mysql查询?

如果我在sql server中使用了相同的语法,则出现以下错误。


  'IN'关键字附近的语法错误


select count(1) as total_record
from sms_allocation
left join sms_api_definition on sms_allocation.sms_api_definition_id = sms_api_definition.sms_api_definition_id
where if(('1' = 1 || '4,26,28,31,32,33,37,41,53,56,58,62,63,66,71,72,73,75,76,77,81' = 81 || '4,26,28,31,32,33,37,41,53,56,58,62,63,66,71,72,73,75,76,77,81' = 82),
      if('0'!='0',reseller_id in (
        select reseller_id
        from sms_allocation
        where reseller_id in (0)),reseller_id in (
          select reseller_id
          from sms_allocation)
        ), reseller_id in (
          select reseller_id
          from sms_allocation
          where reseller_id in (0)) and sms_allocate_to='Company');

最佳答案

只需将mysql if语句替换为标准sql case语句即可解决您的问题。

    select
        count(1) as total_record
    from
        sms_allocation left join sms_api_definition on sms_allocation.sms_api_definition_id = sms_api_definition.sms_api_definition_id
    where
        case when
            '1' = 1 or '4,26,28,31,32,33,37,41,53,56,58,62,63,66,71,72,73,75,76,77,81' = 81 or '4,26,28,31,32,33,37,41,53,56,58,62,63,66,71,72,73,75,76,77,81' = 82
        then
            case when
                '0' != '0'
            then
                reseller_id in (
                    select
                        reseller_id
                    from
                        sms_allocation
                    where
                        reseller_id in (0)
                 )
             else
                 reseller_id in (
                     select
                         reseller_id
                     from
                         sms_allocation
                 )
             end
       else
           reseller_id in (
               select
                   reseller_id
               from
                   sms_allocation
               where
                   reseller_id in (0)
       end
       and
          sms_allocate_to = 'Company'
);


更新:用||替换or,对不起,我错过了。

关于mysql - 'IN'关键字附近的语法错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32044687/

10-11 18:10