【问题标题】:sql list games and total reviews with left join?左连接的sql列表游戏和总评论?
【发布时间】:2014-05-03 00:22:10
【问题描述】:

我正在显示游戏列表并使用左连接表查看。
我只需要对每个游戏的总评论进行输出即可:

array (
[0] => array ('title' => 'game1', 'reviews' => 0), 
[1] => array ('title' => 'game2', 'reviews' => 2), 
[2] => array ('title' => 'game3', 'reviews' => 0), 
) 

我在安装 sql 时遇到问题

我的 sql

select A.title, review.text from game as A
left join game_review as review on review.id_game = A.id
limit 5

我的数据库

tbl-games [id, title]
game1
game2
game3

tbl-review [id, id_game, text]
review of game2
review of game2

【问题讨论】:

  • 你能拍一张你的表的快照或发布一个表结构吗?
  • 为什么将查询限制为 5?
  • 该死的我有权显示为你想要的

标签: php mysql sql join inner-join


【解决方案1】:

这将完成我认为的工作

select game.title, count(game_review.text) from game_review, game where game.id = game_review.id 

select game.title, count(review.text) from game 
left join game_review on game.id = game_review.id_game

【讨论】:

  • 所以我发布了第二个解决方案(自己检查并更正表名)
  • 我将第二个示例代码减少到最低限度,但仍然是相同的逻辑。你用 phpmyadnim 测试过吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-20
  • 1970-01-01
  • 2019-05-19
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
相关资源
最近更新 更多