CodeIgniter - 当secound表中没有行时MySQL连接

时间:2022-10-06 12:18:35

I have this query in model:

我在模型中有这个查询:

  public function Games() {
    $q = $this->db->select('games.id, games.title, games.slug, games.dev_id, games.dev, games.plat_id, games.plat');
    $q = $this->db->from('games');
    $q = $this->db->join('rates', 'games.id = rates.game_id');
    $q = $this->db->select_avg('rates.rate');
    $q = $this->db->get();
    return $q->result();
}

My goal is listing everything from games, additionally getting average rate from rates when available. Now it only shows those rows which are in both tables. How can I solve this?

我的目标是列出游戏中的所有内容,另外还可以获得平均费率。现在它只显示两个表中的那些行。我怎么解决这个问题?

1 个解决方案

#1


5  

Use this instruction

使用此说明

$this->db->select('games.id, games.title, games.slug, games.dev_id, games.dev, games.plat_id, games.plat');
$this->db->select_avg('rates.rate');
$this->db->from('games');
$this->db->join('rates', 'games.id = rates.game_id','left');
$this->db->group_by('rates.game_id');
$q = $this->db->get();

Left join will bring multiple results. using avg and group by will restrict to fetch only one row against each record.

左连接将带来多个结果。使用avg和group by将限制为每条记录仅获取一行。

#1


5  

Use this instruction

使用此说明

$this->db->select('games.id, games.title, games.slug, games.dev_id, games.dev, games.plat_id, games.plat');
$this->db->select_avg('rates.rate');
$this->db->from('games');
$this->db->join('rates', 'games.id = rates.game_id','left');
$this->db->group_by('rates.game_id');
$q = $this->db->get();

Left join will bring multiple results. using avg and group by will restrict to fetch only one row against each record.

左连接将带来多个结果。使用avg和group by将限制为每条记录仅获取一行。