Order_by关于连接表的问题

时间:2017-07-18 06:22:50

标签: mysql codeigniter group-by sql-order-by

我的查询是这个我想按平均评分记录顺序,但它没有按降序获取记录

$this->db->select('doctors.* , speciality.speciality_name , avg(rating.rating) as avg_rating');
        $this->db->from('doctors');
        $this->db->join('speciality' , 'doctors.speciality_id = speciality.id' , 'left');
        $this->db->join('rating' , 'doctors.id = rating.doctor_id' , 'left');
        $this->db->group_by('doctors.id');
        $this->db->order_by('avg_rating' , 'desc');

我根据avg_rating

按此顺序获取记录
  

[avg_rating] => 4.0000

     

[avg_rating] => 4.0000

     

[avg_rating] => 4.5000

     

[avg_rating] => 3.0000

但我希望4.5在顶部

查看显示此数据的文件代码

<ul class="selectedList">
                    <?php if(isset($doctors_data) && !empty($doctors_data)){ foreach($doctors_data as $result){ ?>
                    <li>
                        <div class="row noMargin">
                            <div class="col-lg-2 col-md-2 col-sm-6 col-xs-5 first">
                                <a href="<?php echo base_url(); ?>home_ctrl/detail_page?refer_id=<?php echo $result->id;?>">
                                    <?php if(isset($result->img_name) && !empty($result->img_name)){ ?>
                                    <img src="<?php  echo base_url(); ?>includes/images/doctors/<?php echo $result->img_name; ?>" alt="Best Lady doctors" class="img-responsive doctorImg">
                                    <?php }else{ ?>
                                    <img src="<?php  echo base_url(); ?>includes/images/doctor.jpg" alt="Best Lady doctors" class="img-responsive doctorImg">   
                                    <?php } ?>
                                </a>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-6 col-xs-7 col-md-push-7 third noPadding">
                                <p class="text-right stars" id="<?php echo $result->id; ?>" data-rating="<?php echo round($result->avg_rating); ?>"></p>
                                <p><span class="fa fa-phone tdinnerSpan"></span> <?php echo $result->phone; ?></p>
                                <span class="fa  fa-clock-o tdinnerSpan"></span><p style="width:auto !important" class="innerPara"> <?php echo $result->week_from; ?> - <?php echo $result->week_to; ?> <br> <?php echo date('h:i a', strtotime($result->opening)); ?> - <?php echo date('h:i a', strtotime($result->closing)); ?> </p>
                            </div>
                            <div class="col-lg-7 col-md-7 col-sm-12 col-xs-12 col-md-pull-3 second">
                                <a href="<?php echo base_url(); ?>home_ctrl/detail_page?refer_id=<?php echo $result->id;?>" class="doctorName"><?php echo $result->name; ?>, <small><?php echo $result->education; ?></small></a>
                                <p><span class="fa fa-certificate tdinnerSpan"></span><?php echo $result->speciality_name; ?></p>
                                <span class="fa fa-map-marker tdinnerSpan"></span><p class="innerPara addresPara"><?php echo $result->address; ?></p>
                            </div>
                        </div>
                    </li>
                    <?php } } ?>
                </ul>

2 个答案:

答案 0 :(得分:0)

请更改您的查询,如下所述。

$this->db->select('doctors.* , speciality.speciality_name , CAST(avg(package_rating.rating) as DECIMAL(3,2)) as avg_rating');
$this->db->from('doctors');
$this->db->join('speciality', 'doctors.speciality_id = speciality.id', 'left');
$this->db->join('rating', 'doctors.id = rating.doctor_id', 'left');
$this->db->order_by('avg_rating', 'desc');
$this->db->group_by('doctors.id');

我已经在我的本地机器上尝试过这种方法并且它完美无缺。

$this->db->select('package.* , CAST(avg(package_rating.rating) as DECIMAL(3,2)) as avg_rating');
$this->db->from('package');
$this->db->join('package_rating', 'package.id = package_rating.package_id','left');
$this->db->order_by('avg_rating', 'desc');
$this->db->group_by('package.id');
$data = $this->db->get();

如果不起作用,请告诉我。

答案 1 :(得分:0)

试试这个

$this->db->select('doctors.* , speciality.speciality_name , avg(rating.rating) as avg_rating');
$this->db->from('doctors');
$this->db->join('speciality' , 'doctors.speciality_id = speciality.id' , 'left');
$this->db->join('rating' , 'doctors.id = rating.doctor_id' , 'left');
$this->db->order_by('avg_rating', 'desc'); # or this $this->db->order_by('rating.rating', 'desc'); 
$this->db->group_by('doctors.id');
$query = $this->db->get();
$result = $query->result(); # or $query->result_array();