2013-07-12 134 views
0

您好(我是新來的PHP),排序的查詢結果/ MySQL的子查詢

我不能完全弄清楚一個子查詢我試圖讓的語法,這是查詢:

SELECT * FROM show_episode, shows, show_episode_airdate, show_moyenne 
WHERE season = 1 AND episode = 1 
AND shows.imdb_id = show_episode.imdb_id_show 
AND show_episode_airdate.episode_id = show_episode.episode_id 
AND show_moyenne.show_id = shows.id 
AND show_episode_airdate.airdate < '2013-07-12' 
ORDER BY show_episode_airdate.airdate DESC LIMIT 10 

一旦這樣做,我想訂購由show_moyenne.moyenne那些10個選中的行與類似的東西:

SELECT * (FROM show_episode, shows, show_episode_airdate, show_moyenne 
WHERE season = 1 AND episode = 1 
AND shows.imdb_id = show_episode.imdb_id_show 
AND show_episode_airdate.episode_id = show_episode.episode_id 
AND show_moyenne.show_id = shows.id 
AND show_episode_airdate.airdate < '2013-07-12' 
ORDER BY show_episode_airdate.airdate DESC LIMIT 10) 
* ORDER BY show_moyenne.moyenne DESC 

這是不正確的,任何人都可以告訴我正確的方式做這個 ?

謝謝,任何幫助讚賞!

回答

1
SELECT x.* 
    FROM 
     (SELECT * 
      FROM show_episode e 
      JOIN shows s 
      ON s.imdb_id = e.imdb_id_show 
      JOIN show_episode_airdate a 
      ON a.episode_id = e.episode_id 
      JOIN show_moyenne m 
      ON m.show_id = s.id 
     WHERE season = 1 
      AND episode = 1 
      AND a.airdate < '2013-07-12' 
     ORDER 
      BY a.airdate DESC 
     LIMIT 10 
    ) x 
    ORDER 
    BY moyenne; 
+0

這就是我想要做的,謝謝! 雖然,我得到重複的列episode_id錯誤? – Callombert

+0

請勿使用「SELECT *」。命名您實際想要返回的列。 – Strawberry

+0

我會嘗試一下。 – Callombert

2
Select * from 
(SELECT * FROM show_episode, shows, show_episode_airdate, show_moyenne 
WHERE season = 1 AND episode = 1 
AND shows.imdb_id = show_episode.imdb_id_show 
AND show_episode_airdate.episode_id = show_episode.episode_id 
AND show_moyenne.show_id = shows.id 
AND show_episode_airdate.airdate < '2013-07-12' 
ORDER BY show_episode_airdate.airdate DESC LIMIT 10) as j 
order by j.moyenne DESC 

我希望這能有一定的幫助。

+0

是的,我改變了它。 –