【问题标题】:MySQL Query equivalent in CodeIgniterCodeIgniter 中的 MySQL 查询等效项
【发布时间】:2015-11-25 04:32:55
【问题描述】:

下面是 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 查询等效?

【问题讨论】:

    标签: php mysql codeigniter codeigniter-2 codeigniter-3


    【解决方案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();
    

    【讨论】:

    • 我没有足够的声誉来支持这个答案。对不起。
    【解决方案2】:

    小修正:

    1) 在 select() 中添加第二个参数 false 以避免反引号。
    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();
    

    让我知道它是否有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多