如何将 MySQL 查询转换为 PHP 代码
Posted
技术标签:
【中文标题】如何将 MySQL 查询转换为 PHP 代码【英文标题】:How to convert MySQL query to PHP code 【发布时间】:2017-10-18 04:00:32 【问题描述】:我正在尝试使用 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;
【问题讨论】:
如果在phpmyadmin中,那么你可以在那里创建。 在codeigniter中获取查询结果:$query = $this->db->query("YOUR QUERY")->result_array(); 【参考方案1】:试试这个:
$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;");
【讨论】:
【参考方案2】:$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();
【讨论】:
【参考方案3】: <?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();
?>
【讨论】:
【参考方案4】:您只需使用带有连接变量的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);
?>
【讨论】:
【参考方案5】:如果你想以 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
【讨论】:
【参考方案6】:我从 2013 年开始使用 CI。下面是在 CodeIgniter 中使用数据库的基本结构。不支持子查询和UNION
,因此您需要在两个数据库中执行SELECT
,然后执行$query = $this->db->query($query1." UNION ".$query2);
,或者使用联合编写您自己的子句。
$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
【讨论】:
以上是关于如何将 MySQL 查询转换为 PHP 代码的主要内容,如果未能解决你的问题,请参考以下文章