您需要通过查找不同的 MySQL 关键字来分析查询,例如 INSERT、SELECT、INTO、VALUES、FROM、WHERE 等。但是,这可能会变得棘手和困难,因为您必须注意单引号、转义字符、等等
我建议您只使用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] =>
)
)
)