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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)