在where

时间:2016-06-14 05:54:27

标签: mysql codeigniter

收到错误信息

Unknown column 'code' in 'where clause'
SELECT * FROM `payment` WHERE `code` = 'ORD00023'

尽管我已经使用了join方法来加入支付表和服务表。在这里我找到了前一行值的解决方案,但它没有得到。

这是我的模特

public function order_balance($order_code)
{
$this->db->query("
SELECT p1.*
      , p2.balance AS previous_balance 
  FROM payment p1 
  JOIN payment p2 
    ON p1.order_id = p2.order_id + 1 
   AND p1.customer_id = p2.customer_id 
  LEFT 
  JOIN services s 
    ON p1.customer_id = s.customer_id 
 ORDER BY p1.id DESC
");
$query = $this->db->get_where('payment p1', array('code' => $order_code));
return $query;
}

这是我的餐桌付款

id  order_id    customer_id  amount     actual_amount   paid_amount     balance     type
25  11            16                        100.00        50.00         50.00       Cash
26  12            16                        200.00        100.00        100.00      Cash
27  13            16                        150.00        100.00        50.00       Cash
28  14            16                        300.00        250.00        50.00       Cash
29  14            16                        170.00        100.00        70.00       Cash
30  15            16         100            170.00        70.00         100.00      Cash
31  16            16         400            500.00        300.00        200.00      Cash

这是桌面服务

id  code      customer_id   particulars     
11  ORD00011    16              phone   
12  ORD00012    16              gdf     
13  ORD00013    16              ghgfh   
14  ORD00014    16               tv     
15  ORD00015    16              ghfg    
16  ORD00016    16               tv     
17  ORD00017    16              gdfg    
18  ORD00018    16              desk    
19  ORD00019    16              gdf  

在这里,我加入了付款表和服务表,但仍未获得

见我的表

id  order_id    customer_id     amount  actual_amount   paid_amount     balance     type
50  31             16           650         750.00        250.00        500.00      Cash
51  1              16           100         600.00        300.00        300.00      Cash
52  2              16           100         400.00        200.00        200.00      Cash
53  3              16           800         1000.00       600.00        400.00      Cash
54  4              15           400         400.00        300.00        100.00      Cash
55  5              15           500         600.00        575.00        25.00       Cash
56  6              16           350         750.00        600.00        150.00      Cash

在此表中,16的customer_id具有余额为25的上一行值,但我希望最后的customer_id 16余额值为先前的customer_id 16值。 例如,我的结果应该是这样的

id     order_id    customer_id   amount   actual_amount   paid_amount   balance     type
56       6         16          350          750.00        600.00        400.00      Cash

4 个答案:

答案 0 :(得分:1)

你想要这个吗?

$this->db->query("SELECT p1.*, p2.balance AS previous_balance FROM payment p1 INNER JOIN payment p2 ON p1.order_id = p2.order_id + 1 AND p1.customer_id = p2.customer_id LEFT JOIN services s ON p1.customer_id = s.customer_id AND s.code = ? ORDER BY p1.id DESC", array($order_code));

答案 1 :(得分:0)

因为你正在写两个不同的查询

要查看两者之后添加$this->db->last_query()。您将看到正在执行两个查询。

//1st query

$this->db->query("SELECT p1.*, p2.balance AS previous_balance FROM payment p1
INNER JOIN payment p2 ON p1.order_id = p2.order_id + 1 AND p1.customer_id 
= p2.customer_id LEFT JOIN services s ON p1.customer_id = s.customer_id 
ORDER BY p1.id DESC");
echo  $this->db->last_query();

//2nd query
$query = $this->db->get_where('payment p1', array('code' => $order_code));
echo  $this->db->last_query();

彼此独立。

试试这个

$query = $this->db->query("SELECT p1.*, p2.balance AS previous_balance FROM payment p1
INNER JOIN payment p2 ON p1.order_id = p2.order_id + 1 AND p1.customer_id 
= p2.customer_id LEFT JOIN services s ON p1.customer_id = s.customer_id 
where code='".$order_code."'  ORDER BY p1.id DESC");

$this->db->select('p1.*, p2.balance AS previous_balance');
$this->db->from('payment p1');
$this->db->join('payment p2','p1.order_id = p2.order_id + 1 AND p1.customer_id 
= p2.customer_id','');
$this->db->join('services s','p1.customer_id = s.customer_id','left');
$this->db->where('s.code',$order_code);
$this->db->order_by('p1.id', 'DESC');
$query = $this->db->get();

答案 2 :(得分:0)

这显示错误,因为您缺少表名的别名,其中列代码可用。替换代码

$query = $this->db->get_where('payment p1', array('code' => $order_code));

$query = $this->db->get_where('payment p1', array('services.code' => $order_code));

$query = $this->db->get_where('payment p1', array('s.code' => $order_code));

答案 3 :(得分:0)

我在模型中所做的这些改变得到了结果

public function order_balance($order_code)
{

  $query=$this->db->query("SELECT payment.*, t3.balance AS pre_balance FROM payment 
  INNER JOIN (SELECT max(id) AS id, customer_id FROM payment GROUP BY customer_id) t ON payment.id = t.id AND payment.customer_id = t.customer_id
  LEFT JOIN (SELECT t1.* FROM payment t1
  WHERE (
    SELECT count(*) FROM payment t2 WHERE t1.customer_id = t2.customer_id AND t1.id <= t2.id
) <= 2
AND t1.id not IN (SELECT max(id) FROM payment GROUP BY customer_id)) t3 ON payment.customer_id = t3.customer_id LEFT JOIN services s ON payment.customer_id = s.customer_id AND s.code = ? ", array($order_code)); 
 return $query;
}