+ -
当前位置:首页 → 问答吧 → 联合查询排序问题

联合查询排序问题

时间:2011-12-15

来源:互联网

select
  *  
  from
  ( Select
   
  o.Net_Id As "netId",
  o.Use_Person As "usePerson",
  o.Use_Date As "useDate",
  o.Use_Type As "useType",
  i.Use_Person As "usePerson1",
  i.Use_Date As "useDate1",
  i.Use_Type As "useType1",
  '' As "applyPerson",
  Null As "applyDate",
  '' As "newStatus"  
  From
  Net_Pki o,
  Net_Pki i  
  Where
  o.Id = i.Use_Id (+)  
  And o.use_type = 'O'  
  And o.Net_Id='201104140010'  
  Union 
  Select
  s.Ass_No As "netId",
  '' As "userPerson",
  Null As "useDate",
  '' As "useType",
  '' As "userPerson1",
  Null As "userDate1",
  '' As "useType1",
  s.Apply_Person As "applyPerson",
  s.Apply_Date As "applyDate",
  s.New_Status As "newStatus"  
  From
  Ass_Status_Apply s  
  Where
  s.Ass_No = '201104140010'  
  And s.New_Status='BF'  
  )  
  Order by
  ID ASC  

在oracle数据库中查询时,报 ID:无效标示符
我在 Select
  o.Net_Id As "netId", 中间加了一个o.Id Ad "id" 报查询块具有不正确的结果列数 这个应该怎么改 才能让最后查询出来的数据按照ID升序排列

作者: wyk0617   发布时间: 2011-12-15

必须的呀, Ass_Status_Apply s 这个表哪个字段对应 o.Id As "id"呀

作者: xpingping   发布时间: 2011-12-15

因为你使用了union,要求2个查询结果集的列数必须一致。

作者: LuiseRADL   发布时间: 2011-12-15

SQL code
select
  *   
  from
  ( Select  
  o.Net_Id As "netId",
  [color=#FF0000]o.Id As "id",[/color]
  o.Use_Person As "usePerson",
  o.Use_Date As "useDate",
  o.Use_Type As "useType",
  i.Use_Person As "usePerson1",
  i.Use_Date As "useDate1",
  i.Use_Type As "useType1",
  '' As "applyPerson",
  Null As "applyDate",
  '' As "newStatus"   
  From
  Net_Pki o,
  Net_Pki i   
  Where
  o.Id = i.Use_Id (+)   
  And o.use_type = 'O'   
  And o.Net_Id='201104140010'   
  Union  
  Select
  s.Ass_No As "netId",
  [color=#FF0000]……[/color]
  '' As "userPerson",
  Null As "useDate",
  '' As "useType",
  '' As "userPerson1",
  Null As "userDate1",
  '' As "useType1",
  s.Apply_Person As "applyPerson",
  s.Apply_Date As "applyDate",
  s.New_Status As "newStatus"   
  From
  Ass_Status_Apply s   
  Where
  s.Ass_No = '201104140010'   
  And s.New_Status='BF'   
  )   
  Order by
  "id" ASC   

红色部分要对应

作者: xpingping   发布时间: 2011-12-15

可以先union得到整个集合后,再进行排序。

作者: LuiseRADL   发布时间: 2011-12-15

这个查询在开始的时候
select
  *  
  from
  ( Select
   
  o.Net_Id As "netId",
  o.Use_Person As "usePerson",
  o.Use_Date As "useDate",
  o.Use_Type As "useType",
  i.Use_Person As "usePerson1",
  i.Use_Date As "useDate1",
  i.Use_Type As "useType1",
  '' As "applyPerson",
  Null As "applyDate",
  '' As "newStatus"  
  From
  Net_Pki o,
  Net_Pki i  
  Where
  o.Id = i.Use_Id (+)  
  And o.use_type = 'O'  
  And o.Net_Id='201104140010'  
  Union  
  Select
  s.Ass_No As "netId",
  '' As "userPerson",
  Null As "useDate",
  '' As "useType",
  '' As "userPerson1",
  Null As "userDate1",
  '' As "useType1",
  s.Apply_Person As "applyPerson",
  s.Apply_Date As "applyDate",
  s.New_Status As "newStatus"  
  From
  Ass_Status_Apply s  
  Where
  s.Ass_No = '201104140010'  
  And s.New_Status='BF'  
  )  
  Order by
  ID ASC  

在oracle数据库中查询时,报 ID:无效标示符
 
这个列数是一样的啊 为什么报ID标示符无效就是排序的时候

作者: wyk0617   发布时间: 2011-12-15

你select出来的结果集 哪来的 ID呀

作者: xpingping   发布时间: 2011-12-15

换成useDate 这个也是报一样的错误。。

作者: wyk0617   发布时间: 2011-12-15

热门下载

更多