dsa45132 2019-04-18 07:35
浏览 32
已采纳

如何从表CodeIgniter和MySql中调查所调查的房屋数量

Here's my SQL question...

I have three tables:

course_subcategory

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

course_category

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

events

------------------------------------------------------------------------------
|event_id|title| project |course_cat_id |course_subcat_id|no_of_house_surveyed
-----------------------------------------------------------------------------

-

I want to show the total number of house surveyed and number of events occured having same course_cat_id and course_subcat_id.

Here is my code.

models

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();
          }

controller

$data['course_cat'] = $this->test_event_summary_model->getCourseCat();enter code here

views

foreach($course_cat->result() as $c){

                if ($c->no_of_events == 0) {
                    continue;
                }
echo "<td>".$c->no_of_events."</td>";

                $total_events += $c->no_of_events;
}
  • 写回答

3条回答 默认 最新

  • douxieqiu0651 2019-04-19 01:43
    关注

    I hope this answer will solve your problem-

    Model

    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;
    }
    

    Controller

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

    View

    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'];
    }
    

    Output

    The output will be something like this-

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line