1

我在 mysql 上运行这个查询

SELECT dranken.naam, evenement.waardeBonnetjes as drankPrijs  
       SUM(drankAantal1) as drankAantal from( 
           SELECT consumpties.aantal as drankAantal1 FROM consumpties, 
                  drankencategorie, consumptiebezoeker 
           WHERE drankencategorie.idCategorie = ? and
                 drankencategorie.idDranken = consumptiebezoeker.idEventDrank 
                 and consumpties.idConsumptieBezoeker = consumptiebezoeker.id 
       ), 
       SUM(totaalAantalBonnetjes1) as totaalAantalBonnetjes from( 
           SELECT dranken.aantalBonnetjes as totaalAantalBonnetjes1 from dranken,        
           evenementdrank, drankencategorie 
           WHERE dranken.id = evenementdrank.idDrank 
           and evenementdrank.idEvent = ? and drankencategorie.idCategorie = ? 
       )
FROM 
       evenement 
WHERE evenement.id = ? and drankencategorie.idCategorie = ? 
      and dranken.id = drankencategorie.idDranken

它给出了错误:每个派生表都必须有自己的别名错误

怎么了?

4

1 回答 1

2
( 
           SELECT consumpties.aantal as drankAantal1 FROM consumpties, 
                  drankencategorie, consumptiebezoeker 
           WHERE drankencategorie.idCategorie = ? and
                 drankencategorie.idDranken = consumptiebezoeker.idEventDrank 
                 and consumpties.idConsumptieBezoeker = consumptiebezoeker.id 
       ) t1

第一个之外的每个选择都必须有一个别名(t1)或编译器可以引用/构建的东西。

或完整答案:

SELECT dranken.naam, evenement.waardeBonnetjes as drankPrijs  
       SUM(drankAantal1) as drankAantal from( 
           SELECT consumpties.aantal as drankAantal1 FROM consumpties, 
                  drankencategorie, consumptiebezoeker 
           WHERE drankencategorie.idCategorie = ? and
                 drankencategorie.idDranken = consumptiebezoeker.idEventDrank 
                 and consumpties.idConsumptieBezoeker = consumptiebezoeker.id 
       ) t1, 
       SUM(totaalAantalBonnetjes1) as totaalAantalBonnetjes from( 
           SELECT dranken.aantalBonnetjes as totaalAantalBonnetjes1 from dranken,        
           evenementdrank, drankencategorie 
           WHERE dranken.id = evenementdrank.idDrank 
           and evenementdrank.idEvent = ? and drankencategorie.idCategorie = ? 
       ) t2
FROM 
       evenement 
WHERE evenement.id = ? and drankencategorie.idCategorie = ? 
      and dranken.id = drankencategorie.idDranken
于 2012-05-08T00:25:27.917 回答