【发布时间】:2018-03-21 06:10:13
【问题描述】:
我正在使用 PHP SQL PARSER
我的代码
<?php
require_once dirname(__FILE__) . '/../src/PHPSQLParser.php';
$sql = 'SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
LEFT JOIN Customers ON Orders.CustomerID=Customers.CustomerID where
Customers.CustomerName = "Siddhu"';
$sql = strtolower($sql);
echo $sql . "\n";
$parser = new PHPSQLParser($sql, true);
echo "<pre>";
print_r($parser->parsed);
?>
我得到如下数组的输出
Array (
[SELECT] => Array
(
[0] => Array
(
[expr_type] => colref
[alias] =>
[base_expr] => orders.orderid
[no_quotes] => orders.orderid
[sub_tree] =>
[delim] => ,
[position] => 7
)
[1] => Array
(
[expr_type] => colref
[alias] =>
[base_expr] => customers.customername
[no_quotes] => customers.customername
[sub_tree] =>
[delim] => ,
[position] => 23
)
[2] => Array
(
[expr_type] => colref
[alias] =>
[base_expr] => orders.orderdate
[no_quotes] => orders.orderdate
[sub_tree] =>
[delim] =>
[position] => 47
)
)
[FROM] => Array
(
[0] => Array
(
[expr_type] => table
[table] => orders
[no_quotes] => orders
[alias] =>
[join_type] => JOIN
[ref_type] =>
[ref_clause] =>
[base_expr] => orders
[sub_tree] =>
[position] => 70
)
[1] => Array
(
[expr_type] => table
[table] => customers
[no_quotes] => customers
[alias] =>
[join_type] => LEFT
[ref_type] => ON
[ref_clause] => Array
(
[0] => Array
(
[expr_type] => colref
[base_expr] => orders.customerid
[no_quotes] => orders.customerid
[sub_tree] =>
[position] => 101
)
[1] => Array
(
[expr_type] => operator
[base_expr] => =
[sub_tree] =>
[position] => 118
)
[2] => Array
(
[expr_type] => colref
[base_expr] => customers.customerid
[no_quotes] => customers.customerid
[sub_tree] =>
[position] => 119
)
)
[base_expr] => customers on orders.customerid=customers.customerid
[sub_tree] =>
[position] => 88
)
)
[WHERE] => Array
(
[0] => Array
(
[expr_type] => colref
[base_expr] => customers.customername
[no_quotes] => customers.customername
[sub_tree] =>
[position] => 146
)
[1] => Array
(
[expr_type] => operator
[base_expr] => =
[sub_tree] =>
[position] => 169
)
[2] => Array
(
[expr_type] => const
[base_expr] => "siddhu"
[sub_tree] =>
[position] => 171
)
)
)
现在我想使用这个数组生成查询。我为什么要这样做,稍后我会在这个数组中添加额外的参数。就像我在 WHERE 子句或表中传递附加条件
例如: 上一个查询
$sql = 'SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
LEFT JOIN Customers ON Orders.CustomerID=Customers.CustomerID where
Customers.CustomerName = "Siddhu"';
现在我想在 where 子句中再传递两个条件,例如 WHERE 条件 Customers.CustomerID = "123" and status = "Active" and created_by = 1;
所以我的最终查询是这样的
$sql = 'SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
LEFT JOIN Customers ON Orders.CustomerID=Customers.CustomerID where
Customers.CustomerName = "Siddhu" AND Customers.CustomerID = "123" and status = "Active" and created_by = 1;
那么我该如何实现呢,或者PHPSQLPARSER中是否有任何函数使用这个数组来生成查询?感谢您的提前,如有任何语法错误,我们深表歉意
【问题讨论】:
-
基于这个数组,你想生成什么样的查询,放置一个示例查询
-
相同的查询。但我在 where 子句上添加了附加条件。假设,在我的查询中,我在这里写了' where Customers.CustomerName = "Siddhu"' 我可以添加一个额外的过滤器,例如'where Customers.CustomerName = "Siddhu" and id =1 and created_on = '2018-03-21''像这样。也许添加额外的连接。所以我创建了一个提到的数组的数组。最后,我想将整个转换为查询@ManiMuthuPandi
-
@Siddhu 您现在面临的问题是什么?既然你有数组,那么构建 SQL 语句有什么问题?你的代码在哪里挣扎?请编辑您的问题以包含尝试将数组转换为 SQL 查询的代码并解释究竟是什么不起作用。
-
问题已更新@Progman
-
传递给附加数组到where条件。然后连接字符串进行查询
标签: php mysql sql-parser