简体   繁体   English

如何使用Codeigniter中的联接从SQL查询中删除表中的冗余数据

[英]How to remove redundant data in table from SQL query with join in codeigniter

CONTROLLER: 控制器:

class adminpanel extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('login_database');
    }

    public function index() {
        $data['h'] = $this->login_database->load_orders();  
        $this->load->view('admin_page', $data);
    }

}

MODEL: 模型:

public function load_orders(){
    $this->db->select('*');    
    $this->db->from('orders');
    $this->db->join('order_detail', 'orders.serial = order_detail.orderid');
    $this->db->join('customers', 'orders.customerid = customers.serial');
    $this->db->order_by('date','DESC');
    $query = $this->db->get();
    $result = $query->result();
        return $result;     
    }
}

VIEW: 视图:

<table border="1" cellpadding="5px" cellspacing="1px" bgcolor="#FFFFFF">  
      <tbody>  
         <tr>  
            <td>Date</td>
            <td>Delivery Time</td> 
            <td>Meal Name</td>
            <td>Quantity</td>  
            <td>Customer Name</td>
            <td>Phone</td>
            <td>Location</td> 
            <td>Address</td>
            <td>Payment Method</td>
         </tr>  
         <?php  foreach ($h as $row)  { ?>
            <tr>  
               <td><?php echo $row->date;?></td>
               <td><?php echo $row->time;?></td>
               <td><?php echo $row->prodname;?></td>   
               <td><?php echo $row->quantity;?></td>
               <td><?php echo $row->name;?></td>
               <td><?php echo $row->phone;?></td>
               <td><?php echo $row->location;?></td>
               <td><?php echo $row->address;?></td>
               <td><?php echo $row->payment;?></td>
            </tr>  
         <?php } ?>  
      </tbody>  
   </table>  

The table being returned has so much redundant data eg one customer has ordered many different meals. 返回的桌子上有太多冗余数据,例如,一位顾客订购了许多不同的饭菜。 it shows same customer name over and over for his meals which he has ordered. 它在订购的饭菜上一遍又一遍地显示相同的客户名称。 How do i eliminate this and show in the table?? 如何消除这种情况并在表格中显示?

Please help. 请帮忙。

try modifying your query like this, I used left join and right join.. 尝试像这样修改您的查询,我使用了左联接和右联接。

$this->db->select('*');    
$this->db->from('orders');
$this->db->join('order_detail', 'orders.serial = order_detail.orderid', 'left');
$this->db->join('customers', 'orders.customerid = customers.serial', 'right');
$this->db->order_by('date','DESC');
$query = $this->db->get();
$result = $query->result();
return $result; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM