【问题标题】:not able to fetch desired rows?无法获取所需的行?
【发布时间】:2013-11-16 09:56:38
【问题描述】:

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

游戏: +--------+-------+------+------------- --------+---------- |游戏ID |游戏名 |蛞蝓 |上传日期 |修改日期 +--------+-------+------+------------- --------+---------- | 104 |闹鬼 |空 | 2013-11-15 22:32:39 | 2013-11-15 22:32:39 | | | 105 |侠盗猎车手 |空 | 2013-11-16 10:13:53 | 2013-11-16 10:13:53 | | | 106 |流行时间之沙| 高分辨率照片| CLIPARTO空 | 2013-11-16 10:22:12 | 2013-11-16 10:22:12 +--------+-------+------+------------- --------+---------- 游戏平台: +-----------------+------------+--------+ |游戏平台ID |平台ID |游戏ID | +-----------------+------------+--------+ | 121 | 4 | 104 | | 122 | 4 | 105 | | 123 | 6 | 106 | +-----------------+------------+--------+ 游戏详情: +---------------+-----------------+--------------+ --------+ |游戏详情ID |游戏描述 |发布日期 |游戏ID | +---------------+-----------------+--------------+ --------+ | 99 |

dsfsd

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

fd

| 2013-11-12 | 105 | | 101 | | 2013-11-14 | 106 | +---------------+-----------------+--------------+ --------+ 平台: +------------+---------------+ |平台ID |平台名称 | +------------+---------------+ | 4 |电脑 | | 5 |的Xbox 360 | | 6 | wii | | 7 |附言 2 | | 8 |附言 3 | +------------+---------------+ 游戏投票: +-------------+------------+------------+--------+ |游戏投票ID |投票价值 |平台ID |游戏ID | +-------------+------------+------------+--------+ | 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 

【问题讨论】:

    标签: mysql sql


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多