如何使用PHP SQL解析器将数组值转换为MySQL查询?

如何使用PHP SQL解析器将数组值转换为MySQL查询?,php,mysql,sql-parser,Php,Mysql,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

我正在使用

我的代码

<?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);
?>
现在我想使用这个数组生成查询。为什么要这样做,稍后我将向该数组添加其他参数。就像我在WHERE子句或表格中传递附加条件一样

例如: 上一个查询

  $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;
现在,我想在where子句中再传递两个条件,比如after where condition Customers.CustomerID=“123”和status=“Active”,并通过=1创建

这里我的最后一个问题是

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate,
       CONCAT("SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate  FROM Orders LEFT JOIN Customers ON Orders.CustomerID=Customers.CustomerID WHERE Customers.CustomerName = \"Siddhu\"", " AND Customers.CustomerID = \"", Customers.CustomerID, " and status = \"Active\" and created_by = 1;")
FROM Orders
LEFT JOIN Customers ON Orders.CustomerID=Customers.CustomerID
WHERE Customers.CustomerName = "Siddhu"

那么,我该如何实现它,或者使用这个数组来生成查询是否有任何函数?感谢您提前提出并为语法错误道歉

老实说,我承认我不能完全理解这个问题。但是试图从我能理解的角度来回答这个问题

我相信您希望使用第一个查询的输出,并使用附加的where子句生成另一个查询。您可能只需要在原始查询本身中添加一个带有CONCAT的简单附加select子句就可以做到这一点。用所需的列连接硬编码的原始查询,并生成动态SQL作为附加的输出列

 $parser = new PHPSQLParser('select 1');

 $creator = new PHPSQLCreator($parser->parsed);

 echo $creator->created;

如果status字段也来自其中一个表,则可以中断CONCAT函数并使用该列名。希望能有帮助

老实说,我承认我没有完全理解这个问题。但是试图从我能理解的角度来回答这个问题

我相信您希望使用第一个查询的输出,并使用附加的where子句生成另一个查询。您可能只需要在原始查询本身中添加一个带有CONCAT的简单附加select子句就可以做到这一点。用所需的列连接硬编码的原始查询,并生成动态SQL作为附加的输出列

 $parser = new PHPSQLParser('select 1');

 $creator = new PHPSQLCreator($parser->parsed);

 echo $creator->created;

如果status字段也来自其中一个表,则可以中断CONCAT函数并使用该列名。希望能有帮助

要从数组构建查询,
PHPSQLParser
有一个
creator
方法

从这里的文档:

有两种方法可以从解析器输出创建语句

使用构造函数

为了方便起见,构造函数只需对提供的解析器树输出调用create()方法

使用create()方法

当然,因为
$parser->parsed
是一个
数组
,所以您可以传递自己的数组

[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
        )
    
    // adding other conditions 

    [3] => Array
        (
            [expr_type] => operator
            [base_expr] => and
            [sub_tree] => 
            [position] => 180
        )
        
    [4] => Array
        (
            [expr_type] => colref
            [base_expr] => customers.CustomerID 
            [no_quotes] => customers.CustomerID 
            [position] => 184
        )

    [5] => Array
        (
            [expr_type] => operator
            [base_expr] => =
            [sub_tree] => 
            [position] => 205
        )

    [6] => Array
        (
            [expr_type] => const
            [base_expr] => "123"
            [sub_tree] => 
            [position] => 207
        )
        
    [7] => Array
        (
            [expr_type] => operator
            [base_expr] => and
            [sub_tree] => 
            [position] => 213
        )
            
    [8] => Array
        (
            [expr_type] => colref
            [base_expr] => status 
            [no_quotes] => status
            [position] => 217
        )

    [9] => Array
        (
            [expr_type] => operator
            [base_expr] => =
            [sub_tree] => 
            [position] => 224
        )

    [10] => Array
        (
            [expr_type] => const
            [base_expr] => "Active"
            [sub_tree] => 
            [position] => 226
        )
        
    [11] => Array
        (
            [expr_type] => operator
            [base_expr] => and
            [sub_tree] => 
            [position] => 235
        )
            
    [12] => Array
        (
            [expr_type] => colref
            [base_expr] => created_by  
            [no_quotes] => created_by 
            [position] => 239
        )

    [13] => Array
        (
            [expr_type] => operator
            [base_expr] => =
            [sub_tree] => 
            [position] => 250
        )

    [14] => Array
        (
            [expr_type] => const
            [base_expr] => 1
            [sub_tree] => 
            [position] => 252
        )
)
要将条件添加到数组中,可以将其添加到条件的
WHERE
数组中

每个条件有3个数组,分别定义
colref
(列名)、
operator
(well..operator)和
const
(值)

棘手的部分是
WHERE
子数组中的
位置,因为您需要指定要将这三者中的每一个插入的确切位置,因此根据您提供的示例中的
WHERE
,您可以看到操作符
=
的位置是
169
(从
0
开始)

检查此工具以查看(从1开始)

基于此,

最后的
WHERE
数组应该是这样的(我不确定您是否需要
[无引号]
键):

PS:我在查询中使用了您提供的多个条件,并删除了缩进和换行符来计算位置,如果您没有所需的字符串输出,则使用这些值,因为这只是一个示例


我希望这对您有所帮助,或者至少给您一个想法和好运。

要从数组中构建查询,
PHPSQLParser
有一个
creator
方法

从这里的文档:

有两种方法可以从解析器输出创建语句

使用构造函数

为了方便起见,构造函数只需对提供的解析器树输出调用create()方法

使用create()方法

当然,因为
$parser->parsed
是一个
数组
,所以您可以传递自己的数组

[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
        )
    
    // adding other conditions 

    [3] => Array
        (
            [expr_type] => operator
            [base_expr] => and
            [sub_tree] => 
            [position] => 180
        )
        
    [4] => Array
        (
            [expr_type] => colref
            [base_expr] => customers.CustomerID 
            [no_quotes] => customers.CustomerID 
            [position] => 184
        )

    [5] => Array
        (
            [expr_type] => operator
            [base_expr] => =
            [sub_tree] => 
            [position] => 205
        )

    [6] => Array
        (
            [expr_type] => const
            [base_expr] => "123"
            [sub_tree] => 
            [position] => 207
        )
        
    [7] => Array
        (
            [expr_type] => operator
            [base_expr] => and
            [sub_tree] => 
            [position] => 213
        )
            
    [8] => Array
        (
            [expr_type] => colref
            [base_expr] => status 
            [no_quotes] => status
            [position] => 217
        )

    [9] => Array
        (
            [expr_type] => operator
            [base_expr] => =
            [sub_tree] => 
            [position] => 224
        )

    [10] => Array
        (
            [expr_type] => const
            [base_expr] => "Active"
            [sub_tree] => 
            [position] => 226
        )
        
    [11] => Array
        (
            [expr_type] => operator
            [base_expr] => and
            [sub_tree] => 
            [position] => 235
        )
            
    [12] => Array
        (
            [expr_type] => colref
            [base_expr] => created_by  
            [no_quotes] => created_by 
            [position] => 239
        )

    [13] => Array
        (
            [expr_type] => operator
            [base_expr] => =
            [sub_tree] => 
            [position] => 250
        )

    [14] => Array
        (
            [expr_type] => const
            [base_expr] => 1
            [sub_tree] => 
            [position] => 252
        )
)
要将条件添加到数组中,可以将其添加到条件的
WHERE
数组中

每个条件有3个数组,分别定义
colref
(列名)、
operator
(well..operator)和
const
(值)

棘手的部分是
WHERE
子数组中的
位置,因为您需要指定要将这三者中的每一个插入的确切位置,因此根据您提供的示例中的
WHERE
,您可以看到操作符
=
的位置是
169
(从
0
开始)

检查此工具以查看(从1开始)

基于此,

最后的
WHERE
数组应该是这样的(我不确定您是否需要
[无引号]
键):

PS:我在查询中使用了您提供的多个条件,并删除了缩进和换行符来计算位置,如果您没有所需的字符串输出,则使用这些值,因为这只是一个示例


我希望这对您有所帮助,或者至少给您一个想法和好运。

基于此数组,您希望生成什么样的查询,请放置一个示例QueryName查询。但我在where条款上增加了附加条件。假设,在我的查询中,我在这里写下了“where Customers.CustomerName=“Siddhu”,我可以添加一个额外的过滤器,如“where Customers.CustomerName=“Siddhu”和id=1,并创建了类似这样的“2018-03-21”。可能会添加额外的连接。所以我创建了一个数组。最后,我想将整个过程转换为查询@ManiMuthuPandi@Siddhu你现在面临的问题是什么?既然您有了数组,那么构建SQL的问题是什么
echo $creator->create($myArray); 
[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
        )
    
    // adding other conditions 

    [3] => Array
        (
            [expr_type] => operator
            [base_expr] => and
            [sub_tree] => 
            [position] => 180
        )
        
    [4] => Array
        (
            [expr_type] => colref
            [base_expr] => customers.CustomerID 
            [no_quotes] => customers.CustomerID 
            [position] => 184
        )

    [5] => Array
        (
            [expr_type] => operator
            [base_expr] => =
            [sub_tree] => 
            [position] => 205
        )

    [6] => Array
        (
            [expr_type] => const
            [base_expr] => "123"
            [sub_tree] => 
            [position] => 207
        )
        
    [7] => Array
        (
            [expr_type] => operator
            [base_expr] => and
            [sub_tree] => 
            [position] => 213
        )
            
    [8] => Array
        (
            [expr_type] => colref
            [base_expr] => status 
            [no_quotes] => status
            [position] => 217
        )

    [9] => Array
        (
            [expr_type] => operator
            [base_expr] => =
            [sub_tree] => 
            [position] => 224
        )

    [10] => Array
        (
            [expr_type] => const
            [base_expr] => "Active"
            [sub_tree] => 
            [position] => 226
        )
        
    [11] => Array
        (
            [expr_type] => operator
            [base_expr] => and
            [sub_tree] => 
            [position] => 235
        )
            
    [12] => Array
        (
            [expr_type] => colref
            [base_expr] => created_by  
            [no_quotes] => created_by 
            [position] => 239
        )

    [13] => Array
        (
            [expr_type] => operator
            [base_expr] => =
            [sub_tree] => 
            [position] => 250
        )

    [14] => Array
        (
            [expr_type] => const
            [base_expr] => 1
            [sub_tree] => 
            [position] => 252
        )
)