mysql - 无法获取所需的行?

标签 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 |
+-------------+------------+------------+--------+

I want to show the records of a particular platform say pc(platformID=4): right now i am using this query and it is only displaying the game which gameID and platformID is saved in game_votes table, I need to show all the records from games(gameID,game_name) wheather they are present in game_votes table or not

  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 

最佳答案

使用左连接

引用:http://dev.mysql.com/doc/refman/5.0/en/join.html

"If there is no matching row for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table. You can use this fact to find rows in a table that have no counterpart in another table"

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

关于mysql - 无法获取所需的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20017068/

相关文章:

Linux系统时区更改后Linux上的Mysql时区更改

sql - 回滚完整数据库一小时

mysql - 使用子查询更新永远不会完成

SQLITE 日期未检查类型

mysql - 在一次查询中检索目标记录和周围的 n 条记录?

MySql 查询按月或年获取数据

mysql - 使用文件排序优化自连接

mysql - 如何在 MySQL 中返回数据透视表输出?

java - 更新查询的 SQL 语法错误

MySQL 如何在一个查询中返回 COUNT 总数以及使用 GROUP BY 的 COUNT