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
以下是用CodeIgnitor编写的查询。
$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查询?
答案 0 :(得分:1)
$ 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 :(得分:0)
稍作修正:
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();
让我知道它是否有效。