【问题标题】:How to remove duplicate row in table [duplicate]如何删除表中的重复行[重复]
【发布时间】:2019-04-09 07:22:13
【问题描述】:

我是 codeigniter 框架的新手,我正在尝试停止表中的重复记录 在我的表记录中重复我想一次显示一条记录

这是我的代码

控制器

  $modelResult = $this->freightBillModel->freightBillByDate($data);
  <table id="freight_table" class="table table-bordered table-striped table-sm" style="display:block; overflow: auto; ">
             <tr>
              <th>SR.No</th>
              <th>Bill No</th>
              <th>pkgs</th>
              <th>freight</th>
             </tr>
             <tbody>
                <?php  $counter = 1;   ?>
                <?php foreach($modelResult as $freight){?>

            <tr>

              <td><?php echo $counter;?></td>
              <td><?php echo $freight['tbb_no'];?></td>
              <?php $singleBill = $this->freightBillModel->fetchBillByNo($freight['tbb_no']);  ?>
              <?php $lr_freight=0; $no_of_pkgs=0; ?>

              <?php foreach ($singleBill as $bill) : ?>
              <?php $lr_freight+=$bill->lr_freight; ?>
              <?php $no_of_pkgs+=$bill->no_of_pkgs; ?>
              <?php endforeach; ?>

              <td><?php echo $no_of_pkgs;?></td>
              <td><?php echo $lr_freight;?></td>


             </tr>
            <?php  $counter++; ?>
            <?php }?>
            </tbody>
          </table>

型号:

        public function freightBillByDate($data){
        extract($data);
        $this->db->select('bn.branch_name as to,ts_tbb.tbb_no,ts_tbb.tbb_id,ts_tbb.bilty_id,ts_tbb.current_time,ts_tbb.status,b.*');
        $this->db->from('ts_tbb');
        $this->db->join('bilty b', 'b.id = ts_tbb.bilty_id');

        $this->db->join('branch bn', 'b.lr_to=bn.branch_id');

        $this->db->where('lr_from',$lr_from);
        $this->db->where('lr_to',$lr_to);


        if($bill_type=="pending_bill"){

               $this->db->where('billed_status','not_billed');

         }else if($bill_type=="bill_register"){

               $this->db->where('billed_status','billed');
         }




        $this->db->order_by('lr_date','desc');


        $query=$this->db->get();
       return $query->result_array();

}
function fetchBillByNo($billNo){
    return $this->db->select('*')->from('ts_tbb')->join('bilty','bilty_id=bilty.id')->where('tbb_no',$billNo)->get()->result();
}[![In this picture first two records are repeat][1]][1]

我想展示独特的记录, 当前表它显示重复的行 请告诉我我的代码哪里错了

【问题讨论】:

    标签: php mysql sql codeigniter


    【解决方案1】:

    如果您想查询不同的行,您可以简单地使用 SELECT DISTINCT 而不是 SELECT。

    【讨论】:

      【解决方案2】:

      您可以在select() 中添加DISTINCT 关键字

      return $this->db
      ->select('DISTINCT YOUR_ID_FIELD')
      ->select('*')
      ->from('ts_tbb')
      ->join('bilty','bilty_id=bilty.id')
      ->where('tbb_no',$billNo)
      ->get()
      ->result();
      

      否则,您可以像这样使用group by

      $this->db->select('*');
      $this->db->group_by('YOUR_ID_FIELD');
      

      【讨论】:

      • 在第一个查询中我想通过“ts_tbb.tbb_no”@pupil 区分
      • $this->db->distinct();如何仅对一列使用 distinct @Rakesh Jakhar
      【解决方案3】:

      保留重复记录不是一个好习惯,您可以通过 SQL 查询删除所有重复记录。

      例如:

      DELETE t1 FROM contacts t1
      INNER JOIN
      contacts t2 
      WHERE
      t1.id < t2.id AND t1.email = t2.email;
      

      以上 SQL 查询将删除重复的电子邮件行并保留最高 id。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-21
        • 2021-05-22
        • 2023-04-10
        • 2013-02-13
        • 2020-03-29
        • 1970-01-01
        • 2019-12-08
        相关资源
        最近更新 更多