将MySQL JOIN查询转换为CodeIgniter查询

时间:2018-03-20 11:00:17

标签: php mysql codeigniter

我正在尝试将此查询转换为代码点火器查询。但我不知道从哪里开始。我是代码点火器的新手。请帮忙。

public function  get_loginsecuritydetails($security_date,$apt_id) {

    $sql = "SELECT s.* , IF( DATE( s.security_date ) = DATE( '$security_date' ) , 1, 0 ) AS loginstatus,a.agency_companyname FROM security s JOIN agency a ON(a.agency_id=s.security_cat) WHERE apartment_id='$apt_id'; ";
    $res=mysqli_query($sql) or die(mysqli_error());
    return $res;
}

3 个答案:

答案 0 :(得分:1)

您可以在codeigniter查询构建器中将其转换为如下所示:

$this->db->select("s.* , IF( DATE( s.security_date ) = DATE( '$security_date' ) , 1, 0 ) AS loginstatus,a.agency_companyname");

$this->db->join("agency as a","a.agency_id=s.security_cat");

$this->db->where('apartment_id',$apt_id);

$query = $this->db->get('security as s');

$result = $query->result();

return $result;

答案 1 :(得分:1)

Try like this - 

$this->db->select("s.* , IF( DATE( s.security_date ) = DATE( '$security_date' ) , 1, 0 ) AS loginstatus,a.agency_companyname");
    $this->db->join("agency as a","a.agency_id=s.security_cat");
$this->db->where('apartment_id',$apt_id);
$querys = $this->db->get('security as s');
$result = $querys->result();

答案 2 :(得分:0)

您应该从阅读手册https://www.codeigniter.com/userguide3/database/index.html

开始

说过最简单的解决方案就是简单地运行查询:

$this->db->query('YOUR QUERY HERE');

“YOUR QUERY HERE”部分应替换为实际查询,如:

$this->db->query("SELECT s.* , IF( DATE( s.security_date ) = DATE( '$security_date' ) , 1, 0 ) AS loginstatus,a.agency_companyname FROM security s JOIN agency a ON(a.agency_id=s.security_cat) WHERE apartment_id='$apt_id'");

现在我很确定你想要清理它并开始在查询中使用参数而不是PHP变量。

因此,您应该查看查询构建器类https://www.codeigniter.com/userguide3/database/query_builder.html