JAVA代码里有这样一句SQL语句,我想问一下里面个别 语句 的意思,希望各位不吝赐教!select o.orgid,date_format(o.orderdate,'%Y'),count(*),sum(case when o.finalamount!='0.00' then o.finalamount else o.contractamount end)as totalmoney,sum(case when o.payamount is not NULL then o.payamount else '0.00' end) as paymoney, (sum(case when o.finalamount!='0.00' then o.finalamount else o.contractamount end)-sum(case when o.payamount is not NULL and (case when o.finalamount!='0.00' then o.finalamount else o.contractamount end)!='0.00' then o.payamount else '0.00' end)) as nopaymoney  from htglview o where 1=1 and o.contractState in (5,8,9,10,11)  and o.contractType='0'  group by  o.orgid, date_format(o.orderdate,'%Y') order by o.orgid,date_format(o.orderdate,'%Y')
这里用到了hibernate3作为持久层,所以 o.orgid 就是 查询orgid这个字段,然后表的别名是 o , 是这样么? 然后这句 [color=#FF0000]from htglview o [/color] 我不太懂了,我的数据库里没有htglview这个表,那意思就是新建一个htglview的临时表存入select的数据么?
疑问就是这些,谢谢,或者哪位高手能深入的解释一下这段话,感激不尽。

解决方案 »

  1.   

    上面的代码可能不好看,我从发一下。
    SELECT o.orgid, 
           Date_format(o.orderdate, '%Y'), 
           COUNT(*), 
           SUM(CASE 
                 WHEN o.finalamount != '0.00' THEN o.finalamount 
                 ELSE o.contractamount 
               END)                AS totalmoney, 
           SUM(CASE 
                 WHEN o.payamount IS NOT NULL THEN o.payamount 
                 ELSE '0.00' 
               END)                AS paymoney, 
           ( SUM(CASE 
                   WHEN o.finalamount != '0.00' THEN o.finalamount 
                   ELSE o.contractamount 
                 END) - SUM(CASE 
                              WHEN o.payamount IS NOT NULL 
                                   AND ( CASE 
                                           WHEN o.finalamount != '0.00' THEN 
                                           o.finalamount 
                                           ELSE o.contractamount 
                                         END ) != '0.00' THEN o.payamount 
                              ELSE '0.00' 
                            END) ) AS nopaymoney 
    FROM   htglview o 
    WHERE  1 = 1 
           AND o.contractstate IN ( 5, 8, 9, 10, 11 ) 
           AND o.contracttype = '0' 
    GROUP  BY o.orgid, 
              Date_format(o.orderdate, '%Y') 
    ORDER  BY o.orgid, 
              Date_format(o.orderdate, '%Y')  
      

  2.   

    从名字上看,可能是查询 VIEW
      

  3.   

    SHOW CREATE VIEW  htglview
    什么结果
      

  4.   

    新建查询的结果是 :View      CreatView
    htglview  CREAT ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `htglview` AS select `o`.`ID` AS `id`,`o`.`ContractCode` AS `contractCode`,`o`.`OrgId` AS `orgId`,`o`.`ContractType` AS `contractType`,`o`.`ProjectName` AS `projectName`,`o`.`ContractAmount` AS `contractAmount`,`o`.`FinalAmount` AS `finalAmount`,`o`.`OrderDate` AS `orderDate`,`o`.`ContractState` AS `contractState`,`o`.`Contractcompany` AS `contractcompany`,`custa`.`CustomerName` AS `aname`,`custa`.`Alias` AS `aalias`,`custb`.`CustomerName` AS `bname`,`custb`.`Alias` AS `balias`,(select sum(`contractpay`.`PayAmount`) AS `sum(payAmount)` from `contractpay` where ((`contractpay`.`ContractID` = `o`.`ID`) and (`contractpay`.`State` in (2,4,5))) group by `contractpay`.`ContractID`) AS `payAmount`,(select sum(`contractinvoice`.`InvoiceAmount`) AS `sum(invoiceAmount)` from `contractinvoice` where ((`contractinvoice`.`ContractID` = `o`.`ID`) and (`contractinvoice`.`State` in (2,4,5))) group by `contractinvoice`.`ContractID`) AS `invoiceAmount` from ((`contractinfo` `o` join `customerinfo` `custa`) join `customerinfo` `custb`) where ((1 = 1) and (`o`.`PartyAID` = `custa`.`CustomerId`) and (`o`.`PartyBID` = `custb`.`CustomerId`))
      

  5.   

    楼主的问题是什么? 需要解释这个SQL语句?建议自己先解释一下,否则别人根本不知道从哪儿开始解释,解释到如何地步。 解释过粗可能仍然无法明白。解释得过细则又需要打出过多不必要的字