2015-10-28 45 views
1

我有以下查詢:SQL搜索錯誤:每個派生的表必須有它自己的別名

String queryString = "SELECT pk" 
       + " FROM ({{SELECT COUNT(*) as num, {Book.pk} as pk" 
       + " FROM {Rental JOIN Book ON {Rental.product} = {Book.pk}}" 
       + " GROUP BY {Rental.product}, {Book.pk}" 
       + " ORDER BY num DESC LIMIT ?limit}})"; 

當我執行此,我得到的錯誤「每個派生的表必須有它自己的別名」。我錯過了什麼?

回答

1
"SELECT pk" 
      + " FROM ({{SELECT COUNT(*) as num, {Book.pk} as pk" 
      + " FROM {Rental JOIN Book ON {Rental.product} = {Book.pk}}" 
      + " GROUP BY {Rental.product}, {Book.pk}" 
      + " ORDER BY num DESC LIMIT ?limit}}) t"; 
               ^

只需添加一個別名派生表

+0

謝謝你的快速回復,我會嘗試,現在! – Kapparino

1

的問題是從錯誤信息很清楚。您需要子查詢上的別名。

但是,因爲你並不需要一個子查詢,你的方法過於複雜:

SELECT {Book.pk} as pk" + 
" FROM {Rental JOIN Book ON {Rental.product} = {Book.pk}}" + 
" GROUP BY {Rental.product}, {Book.pk}" + 
" ORDER BY COUNT(*) DESC 
" LIMIT ?limit 
相關問題