我正在尝试使用Codeigniter作为框架从数据库获取数据。
我能够提出SQL查询以获得所需的结果,但由于我不熟悉Codeigniter DB语法,我遇到了麻烦。下面是我想要执行的查询。我怎么在PHP中这样做?
任何建议或建议都将不胜感激。谢谢。
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;
答案 0 :(得分:5)
试试这个:
$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;");
答案 1 :(得分:1)
$qry = "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($qry);
$result = $query->result_array();
答案 2 :(得分:1)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "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";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//displaying row
}
} else {
echo "0 results";
}
$conn->close();
?>
答案 3 :(得分:1)
您必须使用mysqli_query()
函数和连接变量。
<?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);
?>
答案 4 :(得分:1)
如果您想以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());
更多信息您可以在此处查看 https://www.codeigniter.com/userguide3/database/query_builder.html
答案 5 :(得分:0)
我从2013年开始使用CI。以下是在CodeIgniter中使用数据库的基本结构。子查询和UNION
不受支持,因此您需要在两个数据库中执行SELECT
,然后执行$query = $this->db->query($query1." UNION ".$query2);
或使用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();
了解更多信息:https://www.codeigniter.com/userguide3/database/query_builder.html