我遇到一个问题,我正在尝试在我的某个模型中运行一些分页代码。
我马上就能解决这个问题
public function current($page = 0){
if($_POST){
}else{
$this->db->start_cache();
$this->db->select('b.orderID, b.orderTracking, b.orderShipped, b.orderShippedOn, b.orderPaid, b.orderPaidOn,
(Select Top 1 c.payTranID From vwSelectOrderPayments c where c.orderID = b.orderID Order By c.payPosted Desc) as TranID');
$this->db->distinct();
$this->db->from('vwSelectOrders b');
$this->db->where('b.orderPaid', 1);
$this->db->where('b.orderPaidOn >', date('m/d/Y', strtotime('-300 days')));
$this->db->stop_cache();
$total_rows = $this->db->count_all_results();
$this->db->order_by('b.orderPaidOn', 'desc');
$this->db->limit(12, $page);
$qry = $this->db->get();
$rs = $qry->result_array();
$this->db->flush_cache();
$config['base_url'] = '/orders/current';
$config['per_page'] = 12;
$config['total_rows'] = $total_rows;
$config['num_links'] = 5;
$config['first_link'] = '<span class="fa fa-angle-double-left page_num"></span>';
$config['last_link'] = '<span class="fa fa-angle-double-right page_num"></span>';
$config['cur_tag_open'] = '<span class="page_num bold">';
$config['cur_tag_close'] = '</span>';
$config['next_link'] = '<span class="fa fa-angle-right page_num"></span>';
$config['prev_link'] = '<span class="fa fa-angle-left page_num"></span>';
$config['uri_segment'] = 2;
$config['num_tag_open'] = '<span class="page_num">';
$config['num_tag_close'] = '</span>';
$this->pagination->initialize($config);
$data['paging'] = $this->pagination->create_links();
}
$data['rs'] = $rs;
$data['current_page'] = $page;
$this->load->view('templates/header');
$this->load->view('pages/orders/current', $data);
$this->load->view('templates/footer');
}
显示第一页没有问题,但是,点击分页链接后,我看到CI数据库错误:The multi-part identifier "b.orderID" could not be bound.
产生的查询是:
SELECT b.orderID,b.orderTracking,b.orderShipped,b.orderShippedOn,b.orderPaid,b.orderPaidOn,
(Select Top 1 c.payTranID From vwSelectOrderPayments c where c.orderID = b.orderID Order By c.payPosted Desc) as TranID
FROM ( SELECT row_number() OVER (ORDER BY b.orderPaidOn desc) AS
CI_offset_row_number, b.orderID, b.orderTracking, b.orderShipped, b.orderShippedOn, b.orderPaid, b.orderPaidOn,
(Select Top 1 c.payTranID From vwSelectOrderPayments c where c.orderID = b.orderID Order By c.payPosted Desc) as TranID
FROM vwSelectOrders b
WHERE b.orderPaid = 1 AND b.orderPaidOn > '05/21/2013' ) AS A
WHERE A.CI_offset_row_number BETWEEN (13) AND (24)
我尝试在MS SQL Manager中运行,并提供了:
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "b.orderID" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "b.orderTracking" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "b.orderShipped" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "b.orderShippedOn" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "b.orderPaid" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "b.orderPaidOn" could not be bound.
Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "b.orderID" could not be bound.
现在,我在另一个模型中使用几乎完全相同的代码,并且不会发生此问题。 2之间的区别是我查询的视图。
我该怎么做才能解决这个问题?
答案 0 :(得分:1)
您必须使用别名为A
的字段。
而不是使用b.orderID
,b.orderTracking
....您应该使用A.orderID
,A.orderTracking
。
更改您的代码:
$this->db->select('A.orderID, A.orderTracking, A.orderShipped, A.orderShippedOn,A.orderPaid, A.orderPaidOn,
(Select Top 1 c.payTranID From vwSelectOrderPayments c where c.orderID = b.orderID Order By c.payPosted Desc) as TranID');