1

我想获取 SQL 查询的 SELECT 块,而我在想的是获取单词 SELECT 和 FROM 之间的文本。我应该使用什么正则表达式?

是否可以以相同的方式提取 FROM、WHERE 和 JOIN 块?

更新

我不能使用http://code.google.com/p/php-sql-parser/因为它是为 MySQL 构建的,而我的 MSSQL 查询会破坏它。我已经搜索了很多 MSSQL 解析器,但找不到现成的解决方案,所以我能想到的唯一方法是自己使用正则表达式解析 SQL,并按照选择块位于 SELECT 和 FROM 之间的原则工作关键词。

例子

对于查询

SELECT STRAIGHT_JOIN a,b,c 
FROM some_table an_alias
WHERE d > 5;

我想要这样的东西被退回:

$result['SELECT'] => 'STRAIGHT_JOIN a,b,c';
$result['FROM'] => 'some_table an_alias';
$result['WHERE'] => 'd>5';
4

3 回答 3

3

您可以使用 SQL 解析器库为您执行此操作。

签出这个项目http://code.google.com/p/php-sql-parser/

样本输入:

SELECT STRAIGHT_JOIN a,b,c 
  from some_table an_alias
 WHERE d > 5;

样本输出:

Array
( 
[OPTIONS] => Array
    (
        [0] => STRAIGHT_JOIN
    )       

[SELECT] => Array
    (
        [0] => Array
            (
                [expr_type] => colref
                [base_expr] => a
                [sub_tree] => 
                [alias] => `a`
            )

        [1] => Array
            (
                [expr_type] => colref
                [base_expr] => b
                [sub_tree] => 
                [alias] => `b`
            )

        [2] => Array
            (
                [expr_type] => colref
                [base_expr] => c
                [sub_tree] => 
                [alias] => `c`
            )

    )

[FROM] => Array
    (
        [0] => Array
            (
                [table] => some_table
                [alias] => an_alias
                [join_type] => JOIN
                [ref_type] => 
                [ref_clause] => 
                [base_expr] => 
                [sub_tree] => 
            )

    )

[WHERE] => Array
    (
        [0] => Array
            (
                [expr_type] => colref
                [base_expr] => d
                [sub_tree] => 
            )

        [1] => Array
            (
                [expr_type] => operator
                [base_expr] => >
                [sub_tree] => 
            )

        [2] => Array
            (
                [expr_type] => const
                [base_expr] => 5
                [sub_tree] => 
            )

    )

)
于 2012-08-20T08:04:16.423 回答
2

看看这个链接:

http://code.google.com/p/php-sql-parser/

Example Output

Example Query

SELECT STRAIGHT_JOIN a,b,c 
  from some_table an_alias
 WHERE d > 5;

Example Output (via print_r)

Array
( 
    [OPTIONS] => Array
        (
            [0] => STRAIGHT_JOIN
        )       

    [SELECT] => Array
        (
            [0] => Array
                (
                    [expr_type] => colref
                    [base_expr] => a
                    [sub_tree] => 
                    [alias] => `a`
                )

            [1] => Array
                (
                    [expr_type] => colref
                    [base_expr] => b
                    [sub_tree] => 
                    [alias] => `b`
                )

            [2] => Array
                (
                    [expr_type] => colref
                    [base_expr] => c
                    [sub_tree] => 
                    [alias] => `c`
                )

        )

    [FROM] => Array
        (
            [0] => Array
                (
                    [table] => some_table
                    [alias] => an_alias
                    [join_type] => JOIN
                    [ref_type] => 
                    [ref_clause] => 
                    [base_expr] => 
                    [sub_tree] => 
                )

        )

    [WHERE] => Array
        (
            [0] => Array
                (
                    [expr_type] => colref
                    [base_expr] => d
                    [sub_tree] => 
                )

            [1] => Array
                (
                    [expr_type] => operator
                    [base_expr] => >
                    [sub_tree] => 
                )

            [2] => Array
                (
                    [expr_type] => const
                    [base_expr] => 5
                    [sub_tree] => 
                )

        )

)
于 2012-08-20T08:03:57.533 回答
2
$matches = array();
$sql = 'SELECT STRAIGHT_JOIN a,b,c FROM some_table an_alias WHERE d > 5;';
preg_match_all('/SELECT(.+?)FROM(.+?)(?:JOIN(.+?))*WHERE(.+)/gis', $sql, $matches);

会产生:

[0] => Array
    (
        [0] => SELECT STRAIGHT_JOIN a,b,c FROM some_table an_alias WHERE d > 5;
    )

[1] => Array
    (
        [0] =>  STRAIGHT_JOIN a,b,c 
    )

[2] => Array
    (
        [0] =>  some_table an_alias 
    )

[3] => Array
    (
        [0] => 
    )

[4] => Array
    (
        [0] =>  d > 5;
    )
于 2012-08-20T08:04:16.353 回答