如何汇总表CodeIgniter和MySql中被调查的房屋数量

时间:2019-04-18 07:35:18

标签: php mysql codeigniter

这是我的SQL问题...

我有三个表:

course_subcategory

if type(i) == int:
     do_this()

course_category

if isinstance(i, int):
     do_this()

事件

-------------------------------------------------------
| course_subcat_id | course_cat_id| subcoursename| code| 
-------------------------------------------------------

-

我想显示被调查房屋的总数和发生的事件数 具有相同的course_cat_id和course_subcat_id。

这是我的代码。

  

模型

-------------------------------------------------------
| course_cat_id |  coursename| code| project  |deleted|
-------------------------------------------------------
  

控制器

$ data ['course_cat'] = $ this-> test_event_summary_model-> getCourseCat(); ------------------------------------------------------------------------------ |event_id|title| project |course_cat_id |course_subcat_id|no_of_house_surveyed -----------------------------------------------------------------------------

  

观看次数

function getCourseCat(){
        $this->db->select('COUNT(e.title) as no_of_events, SUM(e.no_of_house_surveyed) as total_house_surveyed, c.course_cat_id, c.coursename,s.course_subcat_id,
                  s.subcoursename' )
                  ->from('events e')
                  ->join('course_category c ON e.course_cat_id = c.course_cat_id')
                  ->join('course_subcategory s ON e.course_subcat_id = s.course_subcat_id')
                  ->group_by('e.event_id');
          $result=$this->db->get()->result();
          }

3 个答案:

答案 0 :(得分:0)

请尝试以下操作:

...
POST Request to URL [https://eu-gb.appid.cloud.ibm.com/oauth/v4/4bef41a0-fafa-4a39-87b2-34e3d0a9a288/token
...
[4/18/19 7:56:29:187 UTC] 000000c1 RelyingPartyU <  getData returns
...
<p>The owner of this website (eu-gb.appid.cloud.ibm.com) has banned your access based on your browser's signature (4c95150c9d6d9abe-ua21).</p>

希望这可以解决您的问题。

答案 1 :(得分:0)

尝试一下

function getCourseCat() {
    $this->db->select('COUNT(e.title) as no_of_events, SUM(e.no_of_house_surveyed) as total_house_surveyed, c.course_cat_id, c.coursename,s.course_subcat_id, s.subcoursename' )
      ->from('events as e')
      ->join('course_category as c', 'e.course_cat_id = c.course_cat_id')
      ->join('course_subcategory as s', 'e.course_subcat_id = s.course_subcat_id')
      ->group_by('e.event_id');
        $result = $this->db->get();
        return $result;
}

答案 2 :(得分:0)

我希望这个答案能解决您的问题-

  

型号

function getCourseCat(){
    $where = "e.course_cat_id = c.course_cat_id AND e.course_subcat_id = s.course_subcat_id";
    $this->db->select('c.course_cat_id,c.coursename,s.course_subcat_id,s.subcoursename,COUNT(e.title) as no_of_events,SUM(e.no_of_house_surveyed) as total_house_surveyed');
    $this->db->from('events e');
    $this->db->from('course_category c');
    $this->db->from('course_subcategory s');
    $this->db->where($where);
    $this->db->group_by(array("c.course_cat_id", "c.coursename", "s.course_subcat_id", "s.subcoursename"));
    $query = $this->db->get();
    $res = $query->result_array();
    return $res;
}
  

控制器

public function index(){
    $this->load->model('test_event_summary_model');
    $res['results'] = $this->test_event_summary_model->getCourseCat();
    $this->load->view('cat_view',$res);
} 
  

查看

foreach ($results as $res) {
    echo "<b>course_cat_id</b> - ".$res['course_cat_id']."<br>";
    echo "<b>coursename</b> - ".$res['coursename']."<br>";
    echo "<b>course_subcat_id</b> - ".$res['course_subcat_id']."<br>";
    echo "<b>subcoursename</b> - ".$res['subcoursename']."<br>";
    echo "<b>no_of_events</b> - ".$res['no_of_events']."<br>";
    echo "<b>total_house_surveyed</b> - ".$res['total_house_surveyed'];
}
  

输出

The output will be something like this-