我正在使用Codeigniter进行查询,我正试图让它看起来干净整洁。我还没有添加大部分字段,我可以看到它变得笨拙,特别是使用select_sum。
是否有更清晰的写作方式或组合方式?
$this->db->select('Map');
$this->db->select_sum('Kills')->select_sum('Deaths')->select_sum('Win')->select_sum('Loss')->select_sum('Win + Loss', 'GP');
$this->db->from('games');
$this->db->where('Name', $player_name);
$this->db->join('players', 'players.PlayerID = games.PlayerID');
$this->db->join('gameinfo', 'games.GameID = gameinfo.GameID');
$this->db->group_by('Map');
$this->db->order_by('Kills', "desc");
$this->db->limit(10);
$query = $this->db->get();
return $query->result_array();
答案 0 :(得分:3)
Raw SQL是我的建议。
$name = $this->db->escape($player_name);
$sql = "
SELECT Map, SUM(Kills), SUM(Deaths), SUM(Win), SUM(Loss), SUM(Win + Loss) AS GP
FROM games
JOIN players ON (players.PlayerID = games.PlayerID)
JOIN gameinfo ON (games.GameID = gameinfo.GameID)
WHERE Name = $name
GROUP BY Map
ORDER BY Kills DESC
LIMIT 10
";
$query = $this->db->query($sql);
return $query->result_array();
在我看来,如果只有我们可以做出一个很好的缩进,它比CI活动记录版本更具可读性。
答案 1 :(得分:1)
我使用类似的东西:
$this->db->select("map_name")
->from("maptables")
->where("maptables.id",2);
对我而言,它是可读的:)
(我创建了一个类似于AR的自己的库 - 因为AR有一些错误)
答案 2 :(得分:0)
你也可以用这种方式写它,我认为它更具可读性
$this->db->select('Map, SUM(Kills), SUM(Deaths), SUM(Win), SUM(Loss), SUM(Win + Loss, GP)');
$this->db->from('games');<br />
$this->db->where('Name', $player_name);<br />
$this->db->join('players', 'players.PlayerID = games.PlayerID');<br />
$this->db->join('gameinfo', 'games.GameID = gameinfo.GameID');<br />
$this->db->group_by('Map');<br />
$this->db->order_by('Kills', "desc");<br />
$this->db->limit(10);<br />
$query = $this->db->get();
return $query->result_array();`