views:

142

answers:

1
SELECT gameratingstblx245v.gameid,avg( gameratingstblx245v.rating ) as avgrating, count(gameratingstblx245v.rating) as count,gamedata.name ,gamedata.gameinfo
FROM gameratingstblx245v 
LEFT JOIN gamedata ON gamedata.id = gameratingstblx245v.game_id 
WHERE gameratingstblx245v.game_id=gameratingstblx245v.game_id 
GROUP BY gameid 
ORDER BY avg( gameratingstblx245v.rating ) DESC LIMIT 0,8

Table gameratingstblx245v - gameid, rating
Rable gamedata - id, gameinfo, name, releasedate

This is the query I am currently using to extract data from two tables gamedata and gameratingstblx245v.... What I am doing here is taking the avg. of all the ratings from table gameratingstblx245v in descending order of their avg. rating and I am also extracting the related info corresponding to the selected gameid's from table gamedata...

Now what I want to extract is the top avg. ratings from game_ratingstblx245v but for the games whose field releasedate from table gamedata is in the last 90 days... Help would be appreciated..Thanks

+4  A: 

Here's how I'd design that query:

SELECT d.id, d.name, d.gameinfo,
  AVG(r.rating) AS avgrating, COUNT(r.rating) AS count
FROM gamedata d
LEFT JOIN gameratingstblx245v r ON (d.id = r.game_id)
WHERE d.releasedate BETWEEN NOW() - INTERVAL 90 DAY AND NOW()
GROUP BY d.id 
ORDER BY avgrating DESC LIMIT 0,8;
Bill Karwin
I am getting a syntax error on this query:(
halocursed
It works now..Thanks a lot!
halocursed
Yes sorry I forgot the `INTERVAL` keyword at first. I tested the query and found my mistake. You can also use a MySQL-specific function like `DATEDIFF()` but the `INTERVAL` syntax works the same and it's more standard SQL.
Bill Karwin