无法获取所需的行?

时间:2013-11-16 09:56:39

标签: mysql sql

我正在开发一个游戏网站,任何人都可以对游戏进行排名。 以下是我网站的表格。

games:
+--------+-------------------+------+---------------------+---------------------

| gameID | game_name         | slug | date_uploaded       | date_modified

+--------+-------------------+------+---------------------+---------------------

|    104 | farcry            | NULL | 2013-11-15 22:32:39 | 2013-11-15 22:32:39
|        |
|    105 | gta               | NULL | 2013-11-16 10:13:53 | 2013-11-16 10:13:53
|        |
|    106 | pop sands of time | NULL | 2013-11-16 10:22:12 | 2013-11-16 10:22:12

+--------+-------------------+------+---------------------+---------------------
game_platforms:
+-----------------+------------+--------+
| game_platformID | platformID | gameID |
+-----------------+------------+--------+
|             121 |          4 |    104 |
|             122 |          4 |    105 |
|             123 |          6 |    106 |
+-----------------+------------+--------+
game_details:
+---------------+-----------------+--------------+--------+
| game_detailID | game_desciption | release_date | gameID |
+---------------+-----------------+--------------+--------+
|            99 | 

dsfsd

| 0000-00-00 | 104 | | 100 |

fd

| 2013-11-12 | 105 | | 101 | | 2013-11-14 | 106 | +---------------+-----------------+--------------+--------+
platforms:
+------------+---------------+
| platformID | platform_name |
+------------+---------------+
|          4 | pc            |
|          5 | xbox 360      |
|          6 | wii           |
|          7 | ps 2          |
|          8 | ps 3          |
+------------+---------------+
game_votes:
+-------------+------------+------------+--------+
| game_voteID | vote_value | platformID | gameID |
+-------------+------------+------------+--------+
|           1 |          5 |          4 |    105 |
|           2 |          3 |          6 |    106 |
+-------------+------------+------------+--------+

我想显示特定平台的记录pc(platformID=4): 现在我正在使用这个查询,它只显示游戏哪个gameID和platformID保存在game_votes表中,我需要显示来自游戏(gameID,game_name)的所有记录,它们是否存在于game_votes表中或不是

  select games.gameID,games.game_name,platforms.platform_name,sum(vote_value) as sum_of_votes
  from games 
  inner join game_platforms on game_platforms.gameID=games.gameID 
  inner join platforms on platforms.platformID=game_platforms.platformID 
  inner join game_votes on games.gameID=game_votes.gameID 
  inner join game_details on game_details.gameID=games.gameID 
  where platforms.platformID=4 
  and game_votes.platformID=4 
  and game_votes.gameID=games.gameID 
  and game_details.release_date <= CURRENT_DATE() 
  group by game_votes.gameID 
  order by sum_of_votes desc 

1 个答案:

答案 0 :(得分:1)

使用左连接

参考:http://dev.mysql.com/doc/refman/5.0/en/join.html

  

“如果ON或USING中的右表没有匹配的行   在LEFT JOIN中,使用所有列设置为NULL的行   合适的桌子。您可以使用此事实查找表中的行   在另一张表中没有对应的“

select games.gameID,games.game_name,platforms.platform_name,sum(vote_value) as sum_of_votes
from games
  inner join game_platforms on game_platforms.gameID=games.gameID
  inner join platforms on platforms.platformID=game_platforms.platformID
  inner join game_details on game_details.gameID=games.gameID
  left join game_votes on games.gameID=game_votes.gameID
where platforms.platformID=4
  and game_votes.platformID=4 
  and game_votes.gameID=games.gameID
  and game_details.release_date <= CURRENT_DATE() group by game_votes.gameID
order by sum_of_votes desc
相关问题