mysql中的按问题排序

mysql中的按问题排序,mysql,Mysql,这里总共有两个联合绑定三个查询,第一个查询从虚拟表检索记录(内部查询),两个简单查询从物理表检索记录。 所有三个查询都使用GROUPBY和ORDERBY子句生成一个名为“vertulatable”的虚拟表,从“vertualtable”检索所有记录。 但问题是它没有按最终价格进行适当的降序。从中介器查询检索到的某些记录的值为150,但显示在0或小于150之后,但在结果记录中应位于0或小于150的顶部 SELECT sum(vertualtable.WyoPrice) as WyoPrice,

这里总共有两个联合绑定三个查询,第一个查询从虚拟表检索记录(内部查询),两个简单查询从物理表检索记录。 所有三个查询都使用GROUPBY和ORDERBY子句生成一个名为“vertulatable”的虚拟表,从“vertualtable”检索所有记录。 但问题是它没有按最终价格进行适当的降序。从中介器查询检索到的某些记录的值为150,但显示在0或小于150之后,但在结果记录中应位于0或小于150的顶部

SELECT 
sum(vertualtable.WyoPrice) as WyoPrice, 
       sum(vertualtable.normalPrice) as normalPrice, 
       vertualtable.sbnam,
       vertualtable.sbwynum, 
      if(vertualtable.sbwynum in(25,43), 
           sum(vertualtable.WyoPrice), sum(vertualtable.normalPrice)) as finalPricePrev,
        sum(vertualtable.finalPrice) as  finalPrice,
     vertualtable.BuilderStatus

 FROM (


SELECT sum(vertualtbl.WyoPrice) as WyoPrice, 
       sum(vertualtbl.normalPrice) as normalPrice, 
       vertualtbl.sbnam,
       vertualtbl.sbwynum, 
      if(vertualtbl.sbwynum in(25,43), 
           sum(vertualtbl.WyoPrice), sum(vertualtbl.normalPrice)) as finalPrice,
     'Main' as BuilderStatus
FROM(

  select 
        b.sbnam,
        b.sbwynum,
        'g'        as g,
        sum(if (s.adjprice > 0, s.adjprice, if (s.price > 0, s.price,
         s.estprice))) as normalPrice,
        st.forsearchwoy,
        SUM(if (st.forsearchwoy = 'Y',
               F_offshorePrice(s.topsbwynum, s.hulsbwynum, s.sbwynum, s.adjprice,
            s.price, s.estprice, s.pricehulint, s.pricetopint, s.pricehulcons,
             s.pricetop, s.priceint, s.pricetht),0)) as WyoPrice
 from tblship s
      left join tblbuilder b on b.sbwynum = s.sbwynum and b.deleted = 'N'
      left join tblshiptype as st on st.wytypid = s.wytypid
 where s.deleted != 'Y' and
      (s.sbwynum > 0) and
       status in ('O', 'FO', 'JV', 'PR', 'C', 'F')

 group by s.tblshipwynum



 ) vertualtbl group by vertualtbl.sbwynum 




UNION 

  select 
        '0' as WyoPrice,
        '0' as normalPrice,
        b.sbnam as sbnam,        
        s.hulsbwynum as sbwynum,
        SUM(if (st.forsearchwoy = 'Y',s.pricehulcons,0)) as finalPrice,
        'HUL' as BuilderStatus
 from tblship s
      left join tblbuilder b on b.sbwynum = s.hulsbwynum and b.deleted = 'N'
      left join tblshiptype as st on st.wytypid = s.wytypid
 where s.deleted != 'Y' and
      (s.sbwynum != s.hulsbwynum and
       s.hulsbwynum > 0 ) and
       status in ('O', 'FO', 'JV', 'PR', 'C', 'F')
 group by s.hulsbwynum



  UNION

  select 
       '0' as WyoPrice,
       '0' as normalPrice,
        b.sbnam as sbnam,        
        s.topsbwynum as sbwynum,        
        SUM(if (st.forsearchwoy = 'Y',s.pricetop,0)) as finalPrice,
        'TOP' as BuilderStatus
 from tblship s
      left join tblbuilder b on b.sbwynum = s.topsbwynum and b.deleted = 'N'
      left join tblshiptype as st on st.wytypid = s.wytypid
 where s.deleted != 'Y' and
      (s.sbwynum != s.topsbwynum and
       s.topsbwynum > 0 ) and
       status in ('O', 'FO', 'JV', 'PR', 'C', 'F')
  group by s.hulsbwynum 

 ) as vertualtable 

 group by vertualtable.sbnam
 order by vertualtable.finalPrice desc 

非常感谢。

我对您的查询做了一些小修改-尝试查看结果是否符合您的预期

SELECT 
sum(vertualtable.WyoPrice) as WyoPrice, 
       sum(vertualtable.normalPrice) as normalPrice, 
       vertualtable.sbnam AS sbnam,
       vertualtable.sbwynum, 
      if(vertualtable.sbwynum in(25,43), 
           sum(vertualtable.WyoPrice), sum(vertualtable.normalPrice)) as finalPricePrev,
        sum(vertualtable.finalPrice) as  finalPrice,
     vertualtable.BuilderStatus

 FROM (


SELECT sum(vertualtbl.WyoPrice) as WyoPrice, 
       sum(vertualtbl.normalPrice) as normalPrice, 
       vertualtbl.sbnam AS sbnam,
       vertualtbl.sbwynum, 
      if(vertualtbl.sbwynum in(25,43), 
           sum(vertualtbl.WyoPrice), sum(vertualtbl.normalPrice)) as finalPrice,
     'Main' as BuilderStatus
FROM(

  select 
        b.sbnam AS sbnam,
        b.sbwynum,
        'g'        as g,
        sum(if (s.adjprice > 0, s.adjprice, if (s.price > 0, s.price,
         s.estprice))) as normalPrice,
        st.forsearchwoy,
        SUM(if (st.forsearchwoy = 'Y',
               F_offshorePrice(s.topsbwynum, s.hulsbwynum, s.sbwynum, s.adjprice,
            s.price, s.estprice, s.pricehulint, s.pricetopint, s.pricehulcons,
             s.pricetop, s.priceint, s.pricetht),0)) as WyoPrice
 from tblship s
      left join tblbuilder b on b.sbwynum = s.sbwynum and b.deleted = 'N'
      left join tblshiptype as st on st.wytypid = s.wytypid
 where s.deleted != 'Y' and
      (s.sbwynum > 0) and
       status in ('O', 'FO', 'JV', 'PR', 'C', 'F')

 group by s.tblshipwynum



 ) vertualtbl group by vertualtbl.sbwynum 




UNION 

  select 
        '0' as WyoPrice,
        '0' as normalPrice,
        b.sbnam as sbnam,        
        s.hulsbwynum as sbwynum,
        SUM(if (st.forsearchwoy = 'Y',s.pricehulcons,0)) as finalPrice,
        'HUL' as BuilderStatus
 from tblship s
      left join tblbuilder b on b.sbwynum = s.hulsbwynum and b.deleted = 'N'
      left join tblshiptype as st on st.wytypid = s.wytypid
 where s.deleted != 'Y' and
      (s.sbwynum != s.hulsbwynum and
       s.hulsbwynum > 0 ) and
       status in ('O', 'FO', 'JV', 'PR', 'C', 'F')
 group by s.hulsbwynum



  UNION

  select 
       '0' as WyoPrice,
       '0' as normalPrice,
        b.sbnam as sbnam,        
        s.topsbwynum as sbwynum,        
        SUM(if (st.forsearchwoy = 'Y',s.pricetop,0)) as finalPrice,
        'TOP' as BuilderStatus
 from tblship s
      left join tblbuilder b on b.sbwynum = s.topsbwynum and b.deleted = 'N'
      left join tblshiptype as st on st.wytypid = s.wytypid
 where s.deleted != 'Y' and
      (s.sbwynum != s.topsbwynum and
       s.topsbwynum > 0 ) and
       status in ('O', 'FO', 'JV', 'PR', 'C', 'F')
  group by s.hulsbwynum 

 ) as vertualtable 

 group by sbnam
 order by finalPrice desc

我对你的查询做了一些小的修改——试着看看结果是否符合你的预期

SELECT 
sum(vertualtable.WyoPrice) as WyoPrice, 
       sum(vertualtable.normalPrice) as normalPrice, 
       vertualtable.sbnam AS sbnam,
       vertualtable.sbwynum, 
      if(vertualtable.sbwynum in(25,43), 
           sum(vertualtable.WyoPrice), sum(vertualtable.normalPrice)) as finalPricePrev,
        sum(vertualtable.finalPrice) as  finalPrice,
     vertualtable.BuilderStatus

 FROM (


SELECT sum(vertualtbl.WyoPrice) as WyoPrice, 
       sum(vertualtbl.normalPrice) as normalPrice, 
       vertualtbl.sbnam AS sbnam,
       vertualtbl.sbwynum, 
      if(vertualtbl.sbwynum in(25,43), 
           sum(vertualtbl.WyoPrice), sum(vertualtbl.normalPrice)) as finalPrice,
     'Main' as BuilderStatus
FROM(

  select 
        b.sbnam AS sbnam,
        b.sbwynum,
        'g'        as g,
        sum(if (s.adjprice > 0, s.adjprice, if (s.price > 0, s.price,
         s.estprice))) as normalPrice,
        st.forsearchwoy,
        SUM(if (st.forsearchwoy = 'Y',
               F_offshorePrice(s.topsbwynum, s.hulsbwynum, s.sbwynum, s.adjprice,
            s.price, s.estprice, s.pricehulint, s.pricetopint, s.pricehulcons,
             s.pricetop, s.priceint, s.pricetht),0)) as WyoPrice
 from tblship s
      left join tblbuilder b on b.sbwynum = s.sbwynum and b.deleted = 'N'
      left join tblshiptype as st on st.wytypid = s.wytypid
 where s.deleted != 'Y' and
      (s.sbwynum > 0) and
       status in ('O', 'FO', 'JV', 'PR', 'C', 'F')

 group by s.tblshipwynum



 ) vertualtbl group by vertualtbl.sbwynum 




UNION 

  select 
        '0' as WyoPrice,
        '0' as normalPrice,
        b.sbnam as sbnam,        
        s.hulsbwynum as sbwynum,
        SUM(if (st.forsearchwoy = 'Y',s.pricehulcons,0)) as finalPrice,
        'HUL' as BuilderStatus
 from tblship s
      left join tblbuilder b on b.sbwynum = s.hulsbwynum and b.deleted = 'N'
      left join tblshiptype as st on st.wytypid = s.wytypid
 where s.deleted != 'Y' and
      (s.sbwynum != s.hulsbwynum and
       s.hulsbwynum > 0 ) and
       status in ('O', 'FO', 'JV', 'PR', 'C', 'F')
 group by s.hulsbwynum



  UNION

  select 
       '0' as WyoPrice,
       '0' as normalPrice,
        b.sbnam as sbnam,        
        s.topsbwynum as sbwynum,        
        SUM(if (st.forsearchwoy = 'Y',s.pricetop,0)) as finalPrice,
        'TOP' as BuilderStatus
 from tblship s
      left join tblbuilder b on b.sbwynum = s.topsbwynum and b.deleted = 'N'
      left join tblshiptype as st on st.wytypid = s.wytypid
 where s.deleted != 'Y' and
      (s.sbwynum != s.topsbwynum and
       s.topsbwynum > 0 ) and
       status in ('O', 'FO', 'JV', 'PR', 'C', 'F')
  group by s.hulsbwynum 

 ) as vertualtable 

 group by sbnam
 order by finalPrice desc