gpt4 book ai didi

mysql - 如何通过加入 codeigniter 从 SQL 查询中删除表中的冗余数据

转载 作者:行者123 更新时间:2023-11-30 22:48:50 25 4
gpt4 key购买 nike

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

}

型号:

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

查看:

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

返回的表有太多冗余数据,例如一位顾客点了很多不同的饭菜。它一遍又一遍地显示相同的客户姓名,用于他订购的餐点。我如何消除它并在表格中显示??

请帮忙。

最佳答案

尝试像这样修改您的查询,我使用了左连接和右连接..

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

关于mysql - 如何通过加入 codeigniter 从 SQL 查询中删除表中的冗余数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28755873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com