MySQL查询如下
select PB.*, P.*, WhoCreated.*, WhoPlacedBid.* from tblprojectbid PB
Inner Join tblproject P on P.projectid = PB.projectid
Inner Join tbluser WhoCreated WhoCreated.UserID = P.WhoCreated
Inner Join tbluser WhoPlacedBid WhoPlacedBid.UserID = PB.WhoCreated
Where PB.FreelancerAwardedProjectStatusID in (4, 5)
and WhoCreated.UserID = 3
and PB.Reviewgiven = 0
下面是用CodeIgniter编写的查询。
$this->_ci->db->select('PB.*, P.*, WhoCreated.*, WhoPlacedBid.*');
$this->_ci->db->from('tblprojectbid PB');
$this->_ci->db->join('tblproject P', 'P.projectid = PB.projectid');
$this->_ci->db->join('tbluser WhoCreated', 'WhoCreated.UserID = P.WhoCreated');
$this->_ci->db->join('tbluser WhoPlacedBid', 'WhoPlacedBid.UserID = PB.WhoCreated');
$this->_ci->db->or_where('PB.FreelancerAwardedProjectStatusID', 4);
$this->_ci->db->or_where('PB.FreelancerAwardedProjectStatusID', 5);
$this->_ci->db->where('WhoCreated.UserID', 5);
$this->_ci->db->where('PB.Reviewgiven', 5);
$query = $this->_ci->db->get();
你能帮忙纠正上面的查询,使它等同于上面的MYSQL查询吗?
$ this-> db-> select()接受可选的第二个参数。如果将其设置为FALSE,CodeIgniter将不会尝试使用反引号来保护您的字段或表名。您不需要重复$ this - > _ ci-> db->直到您不想使用条件语句添加连接或条件。使用where_in()检查具有多个值的相同列名。你可以像这样优化代码:
$this->_ci->db->select('PB.*, P.*, WhoCreated.*, WhoPlacedBid.*',FALSE)
->from('tblprojectbid PB')
->join('tblproject P', 'P.projectid = PB.projectid')
->join('tbluser WhoCreated', 'WhoCreated.UserID = P.WhoCreated')
->join('tbluser WhoPlacedBid', 'WhoPlacedBid.UserID = PB.WhoCreated')
->where_in('PB.FreelancerAwardedProjectStatusID', array(4,5))
->where(array('WhoCreated.UserID' => 5, 'PB.Reviewgiven' => 5))
->get();
小修正:
1)将第二个参数false
添加到select()以避免反引号。
2)使用where_in
$this->_ci->db->select('PB.*, P.*, WhoCreated.*, WhoPlacedBid.*',false);
$this->_ci->db->from('tblprojectbid PB');
$this->_ci->db->join('tblproject P', 'P.projectid = PB.projectid');
$this->_ci->db->join('tbluser WhoCreated', 'WhoCreated.UserID = P.WhoCreated');
$this->_ci->db->join('tbluser WhoPlacedBid', 'WhoPlacedBid.UserID = PB.WhoCreated');
$this->_ci->db->where_in('PB.FreelancerAwardedProjectStatusID', array(4,5));
$this->_ci->db->where('WhoCreated.UserID', 5);
$this->_ci->db->where('PB.Reviewgiven', 5);
$query = $this->_ci->db->get();
让我知道它是否有效。