2011-07-15 10 views
0

で最大日付スタンプで複数のレコードを選択しますあなたは仕事をしているようだ。同じ階級に複数の人がいるかもしれないことに注意してください。はこれが最新であるMySQLの

SELECT * 
    FROM (SELECT users.userid as userid, 
       users.name as name, 
       leagues.league as league, 
       leagues.datestamp AS datestamp, 
       ranking.ranking as ranking, 
       user_profiles.profile_pic as profile_pic, 
       user_profiles.team as team 
      FROM users 
    LEFT JOIN leagues ON users.userid = leagues.userid 
    LEFT JOIN ranking ON users.userid = ranking.userid 
    LEFT JOIN user_profiles ON users.userid=user_profiles.userid 
     WHERE (leagues.datestamp <= '{$date}' 
      AND leagues.ladder = '3' 
      AND ranking.ladder = '3') 
     ORDER BY datestamp DESC) as t1 
GROUP BY userid 
ORDER BY ranking 
+1

数値については一重引用符を含めて、MySQLにそれらの文字列を数値にキャストすることは大いにありません。時にはそれはあなたが望むことをしません。 –

+0

UサブクエリでORDER BY datestamp DESCは必要ありません... – Chandu

+0

すべてのリーグで最大の日付スタンプ、またはリーグごとに最大の日付スタンプを探したいですか? – jtoberon

答えて

1

単純なサブクエリを試しましたか?

SELECT users.userid as userid, 
     users.name as name, 
     leagues.league as league, 
     leagues.datestamp AS datestamp, 
     ranking.ranking as ranking, 
     user_profiles.profile_pic as profile_pic, 
     user_profiles.team as team 
FROM users 
LEFT JOIN leagues ON users.userid = leagues.userid 
LEFT JOIN ranking ON users.userid = ranking.userid 
LEFT JOIN user_profiles ON users.userid=user_profiles.userid 
WHERE leagues.datestamp = (
    SELECT MAX(datestamp) 
    FROM leagues 
    WHERE leagues.ladder = '3' 
     AND leagues.datestamp <= '{$date}' 
) 
    AND leagues.ladder = '3' 
    AND ranking.ladder = '3' 
ORDER BY ranking 
+0

ありがとうLEFT JOINsの基本的な問題に気づいた – atomicharri

関連する問題