Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用codeigniter连接查询以计算行数_Php_Mysql_Codeigniter_Join - Fatal编程技术网

Php 使用codeigniter连接查询以计算行数

Php 使用codeigniter连接查询以计算行数,php,mysql,codeigniter,join,Php,Mysql,Codeigniter,Join,我想计算game\u slopes在game\u created\u slopes中有多少个id\u扇区的特定值。 基本上,我想知道为扇区1创建了多少个斜坡 game\u slopes是通用项目表,game\u created\u slopes是成员创建的实际项目表 我试图进行选择查询,但不确定如何进行计数 $this->db->select('game_slopes.id_slope, game_slopes.id_sector, game_created_slopes.id_sl

我想计算
game\u slopes
game\u created\u slopes
中有多少个
id\u扇区的特定值。
基本上,我想知道为扇区1创建了多少个斜坡

game\u slopes
是通用项目表,
game\u created\u slopes
是成员创建的实际项目表

我试图进行选择查询,但不确定如何进行计数

$this->db->select('game_slopes.id_slope, game_slopes.id_sector, game_created_slopes.id_slope, game_created_slopes.id_player');
$this->db->from('game_slopes, game_created_slopes');
$this->db->join('game_created_slopes as created_slopes_tbl', 'game_slopes.id_slope = created_slopes_tbl.id_slope');
$query = $this->db->get();


CREATE TABLE `game_slopes` (
  `id_slope` int(11) NOT NULL,
  `id_sector` int(11) NOT NULL,
  ....more stuff here....
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE `game_created_slopes` (
  `id_created_slopes` int(11) NOT NULL,
  `id_player` int(11) NOT NULL,
  `id_slope` int(11) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

如果有更好的方法,我愿意

您可以通过三种方式存档此文件

  • 以SQL方式(使用)
  • 以Codeigniter方式(使用)
  • 以PHP方式(使用)

  • 以SQL方式

    $this->db->select('COUNT(game_slopes.id_slope) as totalGames , game_slopes.id_slope, game_slopes.id_sector, game_created_slopes.id_slope, game_created_slopes.id_player');
    
    $query = $this->db->get();
    $result = $query->result_array();
    $count = count($result);
    if (empty($count)) {
        return false;
    } else {
        return $result;
    }
    
    以Codeigniter方式

    $this->db->select('COUNT(game_slopes.id_slope) as totalGames , game_slopes.id_slope, game_slopes.id_sector, game_created_slopes.id_slope, game_created_slopes.id_player');
    
    $query = $this->db->get();
    $result = $query->result_array();
    $count = count($result);
    if (empty($count)) {
        return false;
    } else {
        return $result;
    }
    
    $query=$this->db->get()之后添加此
    $totalGames=$query->num_rows()

    以PHP方式

    $this->db->select('COUNT(game_slopes.id_slope) as totalGames , game_slopes.id_slope, game_slopes.id_sector, game_created_slopes.id_slope, game_created_slopes.id_player');
    
    $query = $this->db->get();
    $result = $query->result_array();
    $count = count($result);
    if (empty($count)) {
        return false;
    } else {
        return $result;
    }
    

    $query->num_rows();完美的我实际上没有想到Codeigniter的方法,我想我是专注于SQL方法的。有哪一种方法比另一种更值得推荐吗?@remyremy没有推荐的方法。我使用所有这些。你喜欢哪一个?挑吧。