使用查询工作正常,但Codeigniter的语法不起作用

时间:2016-01-03 13:00:37

标签: php codeigniter

以下查询工作正常:

$query = $this->db->query("
    select users.id as user_id ,users.username,traction_details.title,
     traction_details.tr_id,tractions.buy_id,tractions.coupon_code,
     tractions.coupon_property_id,tractions.expired
     from
      users,tractions,traction_details,coupon_cats
        WHERE

          tractions.id = traction_details.tr_id
          AND
          users.id = tractions.user_id
          AND
          coupon_cats.coupon_id = tractions.coupon_parent
          AND
          coupon_cats.cat_id = 13
          AND
          tractions.succ = 1

        GROUP BY tractions.buy_id
        ORDER  BY traction_details.tr_id DESC

        limit

        $start,$end
");

$r = $query->result();

return $r ;

但是查询下方不能正常工作:

   $this->db->select('users.id as user_id');
    $this->db->select('users.username');
    $this->db->select('traction_details.title');
    $this->db->select('traction_details.tr_id');
    $this->db->select('tractions.buy_id');
    $this->db->select('tractions.coupon_code');
    $this->db->select('tractions.coupon_property_id');
    $this->db->select('tractions.expired');


    $this->db->from('users,tractions,traction_details,coupon_cats');


    $this->db->where('tractions.id ' , 'traction_details.tr_id');
    $this->db->where('users.id ' , 'tractions.user_id');
    $this->db->where('coupon_cats.coupon_id ' , 'tractions.coupon_parent');
    $this->db->where('coupon_cats.cat_id =' , '13');
    $this->db->where('tractions.succ =' , '1');

    $this->db->group_by('tractions.buy_id');
    $this->db->order_by('traction_details.tr_id', 'desc');

    $this->db->limit($start, $end);


    $result = $this->db->get()->result();

    return $result;

我的问题在哪里?

2 个答案:

答案 0 :(得分:1)

您的查询错误,您不需要重复所有内容..尝试

$q =
$this->db->select('users.id as user_id ,users.username,traction_details.title,
     traction_details.tr_id,tractions.buy_id,tractions.coupon_code,
     tractions.coupon_property_id,tractions.expired')
    ->from('users,tractions,traction_details,coupon_cats')
    ->where('tractions.id = traction_details.tr_id
          AND
          users.id = tractions.user_id
          AND
          coupon_cats.coupon_id = tractions.coupon_parent
          AND
          coupon_cats.cat_id = 13
          AND
          tractions.succ = 1')
    ->group_by('tractions.buy_id')
    ->order_by('traction_details.tr_id', 'desc')
    ->limit($start, $end)
    ->get();

$result = $q->result();
return $result;

答案 1 :(得分:0)

解决。我将where子句更改为join子句:

$this->db->select('users.id as user_id');
$this->db->select('users.username');
$this->db->select('traction_details.title');
$this->db->select('traction_details.tr_id');
$this->db->select('tractions.buy_id');
$this->db->select('tractions.coupon_code');
$this->db->select('tractions.coupon_property_id');
$this->db->select('tractions.expired');


$this->db->from('tractions');


$this->db->join('traction_details' , 'traction_details.tr_id = tractions.id');
$this->db->join('users' , 'tractions.user_id = users.id');
$this->db->join('coupon_cats' , 'tractions.coupon_parent = coupon_cats.coupon_id');
$this->db->where('coupon_cats.cat_id =' , '13');
$this->db->where('tractions.succ =' , '1');

$this->db->group_by('tractions.buy_id');
$this->db->order_by('traction_details.tr_id', 'desc');

$this->db->limit($start, $end);


$result = $this->db->get()->result();

return $result;