开发者

Error with a join query

SELECT r.game, u.username, r.userid, r.points, u.format  
FROM ".TBL_RANKING." r 
    INNER JOIN ".TBL_USERS." u 
    ON u.id = r.userid
WHERE r.type = '1' AND r.game = 
            (SELECT name 
             FROM ".TBL_GAME." 
             WHERE active = '1' 
          开发者_StackOverflow   ORDER BY rand() 
             LIMIT 1) 
     AND u.format = 
           (SELECT name 
            FROM ".TBL_FORMAT." 
        WHERE active = '1' 
            ORDER BY rand() LIMIT 1)
ORDER BY r.points DESC LIMIT 5

This query isn't working as it's supposed to. It's selecting the odd user and sometimes none at all.

The query should: -select a random game from the game table -select a random format from the format table -select users ranked on that game but only from the format selected

so if the random selection was FIFA 12 Xbox 360, it would find all users that are from format type Xbox 360 and are ranked on FIFA 12.

The table structure is as follows:

*tbl_ranking*
 id 
 userid 
 game 
 points 
 type

*tbl_users*
id
username
format

*tbl_game*
id
name

*tbl_format*
id 
name

Can anyone see a problem here?


try to have sub queries using left join

SELECT r.game, u.username, r.userid, r.points, u.format 
  FROM TBL_RANKING r 
 INNER JOIN TBL_USERS u 
    ON u.id = r.userid 
  LEFT JOIN (SELECT name FROM ".TBL_GAME." WHERE active = '1' ORDER BY rand() LIMIT 1)  temp1
    ON r.game=temp1.name
  LEFT JOIN (SELECT name FROM ".TBL_FORMAT." WHERE active = '1' ORDER BY rand() LIMIT 1)  temp2
    ON u.format=temp2.name
 WHERE r.type = '1' 
   AND temp1.name != ''
   AND temp2.name != ''
 ORDER BY r.points DESC LIMIT 5
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜