Postgre SQL   发布时间:2022-05-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了CASE语句是否会丢失PostgreSQL中的别名范围?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
首先,这个sql有效: @H_@R_197_11404@_5@ @H_@R_197_11404@_5@
SELEct
    case
        when s.luserID > 0 then u.szusername
        when s.lgroupID > 0 then g.szgroup
        when s.lldAPId > 0 then 'LDAP Group'
    end as name
from security s
left join users u on s.luserID = u.id
left join usergroups g on s.lgroupID = g.ID
order by name
@H_@R_197_11404@_5@上面的块通过别名工作证明排序,以及声明别名名称有效,而术语名称是保留字,这与问题无关

@H_@R_197_11404@_5@当我在其中创建带有别名的case语句时,我的问题就出现了:

@H_@R_197_11404@_5@注意别名usEID

@H_@R_197_11404@_5@
SELEct
    case
        when sa.luserID > 0 then sa.luserID
        when sa.lgroupID > 0 then sa.lgroupID
        when sa.lldAPId > 0 then sa.lldAPId
    end as usEID,from security s
left join users u on s.luserID = u.id
left join usergroups g on s.lgroupID = g.ID
order by
        case
            when 'user SELEction' = 'all objects by user' then usEID
            else s.lobjectID
        end
@H_@R_197_11404@_5@在运行sql之前,文本用户选择由具有文字文本的解析器替换.别名usEIDs.lobjectID都是bigint类型.

@H_@R_197_11404@_5@当’用户选择’=’用户的所有对象’然后使用usEID时,会引发错误.

@H_@R_197_11404@_5@我是否在CASE声明中丢失了别名usEID的范围?
当我尝试在此处使用别名usEID时,为什么会失败.

@H_@R_197_11404@_5@顺便说一句,这个sql也可以:

@H_@R_197_11404@_5@
SELEct
    case
        when s.luserID > 0 then u.szusername
        when s.lgroupID > 0 then g.szgroup
        when s.lldAPId > 0 then 'LDAP Group'
    end as name
from security s
left join users u on s.luserID = u.id
left join usergroups g on s.lgroupID = g.ID
order by
        case
            when s.lobjectID > 0 then s.lobjectID
            else s.luserID
        end
@H_@R_197_11404@_5@上面的块证明了ORDER BY语句中的CASE语句确实有效.关于上述sql块的逻辑操作的所有争论都与问题无关,因为它只是垃圾示例sql.

解决方法

postgresql中你不能尝试做什么,因为它不允许你在同一个查询中使用AliAS作为字段.与MysqL不同,你可以在那里做到. @H_@R_197_11404@_5@ @H_@R_197_11404@_5@要解决您的问题,您可以将查询创建为子查询,然后您的别名将是一个字段,因此可以用作:

@H_@R_197_11404@_5@
SELEct usEID,lobjectID from (
  SELEct
      case
          when sa.luserID > 0 then sa.luserID
          when sa.lgroupID > 0 then sa.lgroupID
          when sa.lldAPId > 0 then sa.lldAPId
      end as usEID,lobjectID
   from security s
  left join users u on s.luserID = u.id
  left join usergroups g on s.lgroupID = g.ID
  ) as t
order by
        case
            when 'user SELEction' = 'all objects by user' then usEID
            else lobjectID
        end
@H_@R_197_11404@_5@或者你可以重复entiry case块

@H_@R_197_11404@_5@
SELEct
      case
          when sa.luserID > 0 then sa.luserID
          when sa.lgroupID > 0 then sa.lgroupID
          when sa.lldAPId > 0 then sa.lldAPId
      end as usEID,lobjectID
   from security s
  left join users u on s.luserID = u.id
  left join usergroups g on s.lgroupID = g.ID
order by
        case
            when 'user SELEction' = 'all objects by user' then 
                  case
                      when sa.luserID > 0 then sa.luserID
                      when sa.lgroupID > 0 then sa.lgroupID
                      when sa.lldAPId > 0 then sa.lldAPId
                  end
            else lobjectID
        end
@H_@R_197_11404@_5@某些引擎会让您使用选择范围上的字段的订单号,顺序如下:

@H_@R_197_11404@_5@
SELEct a,b,c from sometable order by 1,2
@H_@R_197_11404@_5@这意味着此查询将按字段a和b排序

大佬总结

以上是大佬教程为你收集整理的CASE语句是否会丢失PostgreSQL中的别名范围?全部内容,希望文章能够帮你解决CASE语句是否会丢失PostgreSQL中的别名范围?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:首先