简体   繁体   English

在JOIN子句之后指定WHERE子句-Active Record-CodeIgniter

[英]Specifying a WHERE clause after a JOIN clause - Active Record - CodeIgniter

I wanted to ask is it possible to specify a where clause after a join in order to match data to a users input. 我想问问是否可以在联接后指定where子句,以将数据与用户输入匹配。 Say for example I have the following code: 例如,我有以下代码:

 $this->db->select('user.*,role.*')
  $this->db->from('user');
  $this->db->where('user.username', $username);
  $this->db->where('user.password', $password);
  $this->db->join('role','role.id = user.role_id')
  $result = $this->db->get();

and I want to query the matching data from the table 'role': 我想从表“角色”中查询匹配的数据:

 $this->db->select('user.*,role.*')
  $this->db->from('user');
  $this->db->where('user.username', $username);
  $this->db->where('user.password', $password);
  $this->db->join('role','role.id = user.role_id')
  $this->db->where('role.speaker', $speaker); //want to know if this is correct
  $result = $this->db->get();

Is this possible? 这可能吗? Can I compare results (using WHERE) after a JOIN? 加入后可以比较结果(使用WHERE)吗? Will that produce results matching to the WHERE clause? 会产生与WHERE子句匹配的结果吗?

Thanks! 谢谢!

CodeIgniter doesn't actually build the query until ->get() is called. 直到调用->get() CodeIgniter才真正构建查询。 You can call the methods in whatever order you want. 您可以按任意顺序调用方法。

    $this->db->select(array('tbl_board_type_details.id','tbl_board_type_details.price','tbl_bord_type.bord_type_name'));
    $this->db->from(array('tbl_board_type_details'));
    $this->db->join('tbl_bord_type','tbl_bord_type.id=tbl_board_type_details.board_type_id');
    $this->db->where('tbl_board_type_details.hotel_id', $id);
    $query = $this->db->get();
    $boardtype_result =  $query->result();
    $data['boardtype_result'] = $boardtype_result;

You can try this code. 您可以尝试此代码。 It's working fine .. 工作正常..

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

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