如何将MySQL查询转换为PHP代码
我试图使用Codeigniter作为框架从数据库中获取数据 我能够为期望的结果提出SQL查询,但由于我不熟悉Codeigniter DB语法,所以遇到了一个问题。下面是我要执行的查询。如何在php中实现这一点 如有任何意见或建议,将不胜感激。多谢各位如何将MySQL查询转换为PHP代码,php,mysql,codeigniter,codeigniter-3,Php,Mysql,Codeigniter,Codeigniter 3,我试图使用Codeigniter作为框架从数据库中获取数据 我能够为期望的结果提出SQL查询,但由于我不熟悉Codeigniter DB语法,所以遇到了一个问题。下面是我要执行的查询。如何在php中实现这一点 如有任何意见或建议,将不胜感激。多谢各位 SELECT A.*, B.* FROM (SELECT A.* FROM DriversInfo as A, InvoiceQueue as B WHERE A.CreateTime = B.DriverCreateTim
SELECT A.*, B.*
FROM
(SELECT A.*
FROM DriversInfo as A, InvoiceQueue as B
WHERE A.CreateTime = B.DriverCreateTime
AND B.Error <> 1
GROUP BY A.CreateTime
UNION
SELECT DISTINCT A.*
FROM DriversInfo as A, OrderInfo as B
WHERE A.CreateTime = B.DriverKey
AND B.Invoice <> '0') as A
LEFT JOIN DriversDoc as B
on A.CreateTime = B.DriverCreateTime
WHERE B.DriversLicense is null
OR B.CarRegistration is null
OR B.BizCertificate is null
OR B.Insurance is null;
试试这个:
$query=$this->db->query("SELECT A.*, B.* FROM (SELECT A.* FROM DriversInfo as A, InvoiceQueue as B WHERE A.CreateTime = B.DriverCreateTime AND B.Error <> 1 GROUP BY A.CreateTime UNION SELECT DISTINCT A.* FROM DriversInfo as A, OrderInfo as B WHERE A.CreateTime = B.DriverKey AND B.Invoice <> '0') as A LEFT JOIN DriversDoc as B on A.CreateTime = B.DriverCreateTime WHERE B.DriversLicense is null OR B.CarRegistration is null OR B.BizCertificate is null OR B.Insurance is null;");
您只需使用带有连接变量的mysqli_查询函数
<?php
$sql = "YOUR QUERY";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
//Access like $row["id"]
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
如果你想以codeigniter的方式运行这个查询,那么试试这个
$this->db->select('A.*, B.*')
$this->db->from('(SELECT A.*
FROM DriversInfo as A, InvoiceQueue as B
WHERE A.CreateTime = B.DriverCreateTime
AND B.Error <> 1
GROUP BY A.CreateTime
UNION
SELECT DISTINCT A.*
FROM DriversInfo as A, OrderInfo as B WHERE A.CreateTime = B.DriverKey AND B.Invoice <> '0') as A')
$this->db->join('DriversDoc', 'A.CreateTime = B.DriverCreateTime ', 'left');
$this->db->where('B.DriversLicense', null);
$this->db->or_where('B.CarRegistration', null);
$this->db->or_where('B.BizCertificate', null);
$this->db->or_where('B.Insurance', null);
$this->db->get();
你也可以这样尝试
$query = $this->db->query('SELECT A.*, B.*
FROM
(SELECT A.*
FROM DriversInfo as A, InvoiceQueue as B
WHERE A.CreateTime = B.DriverCreateTime
AND B.Error <> 1
GROUP BY A.CreateTime
UNION
SELECT DISTINCT A.*
FROM DriversInfo as A, OrderInfo as B
WHERE A.CreateTime = B.DriverKey
AND B.Invoice <> '0') as A
LEFT JOIN DriversDoc as B
on A.CreateTime = B.DriverCreateTime
WHERE B.DriversLicense is null
OR B.CarRegistration is null
OR B.BizCertificate is null
OR B.Insurance is null;');
echo print_r($query->result());
有关更多信息,请查看此处
自2013年以来,我一直在与CI合作。下面是在CodeIgniter中使用数据库的基本结构。不支持子查询和UNION,因此您需要在两个数据库中执行SELECT,然后执行$query=$this->db->query$query1。工会2美元;或者使用union编写自己的子句
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table2.id = table1.id'); //same as JOIN table2 ON table2.id = table1.id
$this->db->join('table2', 'table2.id = table1.id', 'left'); //produces a LEFT JOIN table2 ON table2.id = table1.id
$this->db->where('column1', 'data'); //same as WHERE column1 = 'data'
$this->db->where("colum1 = 'data' OR column2 = 'data2'"); // you can write you own clause
$this->db->like('title', 'match'); //same as WHERE title LIKE '%match%'
$this->db->like('title', 'match', 'before'); //same as WHERE title LIKE '%match'
$this->db->like('title', 'match', 'after'); //same as WHERE title LIKE 'match%'
$this->db->like('title', 'match', 'both'); //same as WHERE title LIKE '%match%'
$this->db->group_by("title");
$this->db->order_by('title', 'ASC');
$query = $this->db->get();
有关更多信息:如果在phpmyadmin中,则可以在那里创建。要在codeigniter中获取查询结果,请执行以下操作:$query=$this->db->queryYOUR query->result\u array;
$query = $this->db->query('SELECT A.*, B.*
FROM
(SELECT A.*
FROM DriversInfo as A, InvoiceQueue as B
WHERE A.CreateTime = B.DriverCreateTime
AND B.Error <> 1
GROUP BY A.CreateTime
UNION
SELECT DISTINCT A.*
FROM DriversInfo as A, OrderInfo as B
WHERE A.CreateTime = B.DriverKey
AND B.Invoice <> '0') as A
LEFT JOIN DriversDoc as B
on A.CreateTime = B.DriverCreateTime
WHERE B.DriversLicense is null
OR B.CarRegistration is null
OR B.BizCertificate is null
OR B.Insurance is null;');
echo print_r($query->result());
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table2.id = table1.id'); //same as JOIN table2 ON table2.id = table1.id
$this->db->join('table2', 'table2.id = table1.id', 'left'); //produces a LEFT JOIN table2 ON table2.id = table1.id
$this->db->where('column1', 'data'); //same as WHERE column1 = 'data'
$this->db->where("colum1 = 'data' OR column2 = 'data2'"); // you can write you own clause
$this->db->like('title', 'match'); //same as WHERE title LIKE '%match%'
$this->db->like('title', 'match', 'before'); //same as WHERE title LIKE '%match'
$this->db->like('title', 'match', 'after'); //same as WHERE title LIKE 'match%'
$this->db->like('title', 'match', 'both'); //same as WHERE title LIKE '%match%'
$this->db->group_by("title");
$this->db->order_by('title', 'ASC');
$query = $this->db->get();