2011-07-21 53 views
2

我想查詢我的mysql數據庫中的5個單獨的表,並顯示與分配給他們的項目數量最多的演員。表結構如下;計算分配給用戶的最大數量的項目使用最大

item 
itemid | item | description | brand | date | time | path | 

actor 
actorid | name | actorthumb | bio | 

brand 
brandid | brandname | description | image | 

movie 
movieid | title | genre | year | moviethumb | synopsis| 

request 
requestid | userid | itemid | brandid | movieid | actorid | content | requestdate | 

我相信我需要加入的要求,演員和項目表一起,使用COUNT函數計算有多少項被分配給一個演員,然後使用GREATEST顯示與分配給項目的最高金額演員他們?

The query to join all the tables is 
$query = "SELECT greatest i.*, a.*, b.*, m.*, r.* FROM item AS i, actor AS a, brand AS b, movie AS m, request AS r 
    WHERE r.itemid = i.itemid 
    AND r.actorid = a.actorid 
    AND r.brandid = b.brandid 
    AND r.movieid = m.movieid"; 

請確認最好的方法來做到上述?

回答

1
SELECT a.actorid, a.name 
    FROM request r 
     INNER JOIN actor a 
      ON r.actorid = a.actorid 
    GROUP BY a.actorid, a.name 
    ORDER BY COUNT(DISTINCT r.itemid) DESC 
    LIMIT 5 
+0

OK感謝的是我一旦插入此查詢顯示的內容的最好方式?我正在使用while循環和LIMIT命令? –

+0

while循環的目的是什麼?這應該只返回一個值。 –

+0

我想顯示前5名演員,我們也想讓這些可點擊的,所以當你點擊演員時,你需要演員頁面。我正在使用以下內容; –

0
The query to join all the tables is 
$query = "SELECT i.*, a.*, b.*, m.*, r.* FROM item AS i, actor AS a, brand AS b, movie AS m, request AS r 
    WHERE r.itemid = i.itemid 
    AND r.actorid = a.actorid 
    AND r.brandid = b.brandid 
    AND r.movieid = m.movieid 
    ORDER BY count(i.*) DESC";