2011-07-01 53 views
0

我有了這些表電影數據庫:new_movies,收視率,critic_ratings,顏色有許多表來選擇MySQL的適當的方式參與

我試圖執行此SELECT語句將結合在同這4個表使用 'MID'(電影ID)電影:

SELECT DISTINCT 
    new_movies.*, 
    movies_db.*, 
    ratings.rating, 
    ratings.count,color, 
    critic_ratings.rating AS critic_ratings 
FROM 
    new_movies 
INNER JOIN 
    movies_db 
ON 
    new_movies.mid = movies_db.mid 
LEFT JOIN 
    ratings 
ON 
    new_movies.mid = ratings.mid 
LEFT JOIN 
    colors 
ON 
    new_movies.mid = colors.mid 
LEFT JOIN 
    critic_ratings 
ON 
    new_movies.mid = critic_ratings.mid 
ORDER BY 
    title ASC 

但我得到這個錯誤:

The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET SQL_MAX_JOIN_SIZE=# if the SELECT is okay

如何正確做查詢?

+3

你試過'SET SQL_BIG_SELECTS = 1'還是'SET SQL_MAX_JOIN_SIZE =#'? – Flimzy

回答

1

如果你不想啓用大的選擇,你可以使用相關的子查詢來改革這個。 (我不知道你是否仍然會打的限制或不雖然)。

SELECT DISTINCT 
    new_movies.*, 
    movies_db.*, 
    (SELECT rating FROM ratings  WHERE new_movies.mid = ratings.mid) AS rating, 
    (SELECT count FROM ratings  WHERE new_movies.mid = ratings.mid) AS rating_count, 
    (SELECT color FROM colors   WHERE new_movies.mid = colors.mid) AS colour, 
    (SELECT rating FROM critic_ratings WHERE new_movies.mid = critic_ratings.mid) AS critic_ratings 
FROM 
    new_movies 
INNER JOIN 
    movies_db 
    ON new_movies.mid = movies_db.mid 
ORDER BY 
    title ASC 

此外,值得一試,看看是否LEFT JOIN的實際上的原因,您可以執行以下?

SELECT DISTINCT 
    new_movies.*, 
    movies_db.* 
FROM 
    new_movies 
INNER JOIN 
    movies_db 
    ON new_movies.mid = movies_db.mid 
ORDER BY 
    title ASC 
0

爲什麼你有電影和new_movies表?肯定一個發佈日期字段就足夠了 - 也會刪除一個加入...

爲此,我會創建一個數據視圖並查詢,而不是。

但是,回到你的查詢:

SELECT DISTINCT 
    new_movies.*, 
    movies_db.*, 
    ratings.rating, 
    ratings.count, 
    color, 
    critic_ratings.rating AS critic_ratings 
FROM 
    new_movies 
INNER JOIN 
    movies_db 
ON 
    new_movies.mid = movies_db.mid 
LEFT JOIN 
    ratings 
ON 
    new_movies.mid = ratings.mid 
LEFT JOIN 
    colors 
ON 
    new_movies.mid = colors.mid 
LEFT JOIN 
    critic_ratings 
ON 
    new_movies.mid = critic_ratings.mid 
ORDER BY 
    title ASC 

我什麼都看不到明顯的...也許你可以發佈一個解釋查詢的結果?

0

查詢本身沒有問題。只是你選擇了所有的電影(沒有,沒有限制),並且因爲你加入了評級,例如它會將所有評級加入到每部電影中。您只是達到允許連接的最大數據量。

我不確定爲什麼你需要選擇所有電影。也許你可以使用一個限制。否則,您可以嘗試錯誤消息中的解決方案。