我有一个非常长的查询,里面有很多子查询(差不多有一千行)。
我想要做的是获取查询中使用的所有表的名称。因此,请参阅以下查询示例,以获得一个想法:
$sql = "SELECT
...,
...,
(
SELECT COUNT(t.id) AS t_count
FROM _user_just_a_table_db_ jat
WHERE ...
AND ...
AND ...
),
...,
...,
...
FROM _user_main_table_db_ tbl
LEFT JOIN _user_joining_table_db_ jt
ON jt.id = tbl.jt_id
WHERE ...
AND ...
AND tbl.id IN (
SELECT at.id
FROM _user_another_table_db_ at
WHERE at.active = 'Y'
)";
在这种特殊情况下,我希望得到array
或string
,其结果如下:
just_a_table
main_table
joining_table
another_table
array
或string
轻松删除重复项。_user_
和_db_
之间。答案 0 :(得分:2)
这应该有效
_user_(.*?)_db_
<强> http://localhost:yourport/someApp/# 强>
PHP代码
$match = array();
$re = "/_user_(.*?)_db_/";
$str = "SELECT\n ...,\n ...,\n (\n SELECT COUNT(t.id) AS t_count\n FROM _user_just_a_table_db_ jat\n WHERE ...\n AND ...\n AND ...\n ),\n ...,\n ...,\n ...\n FROM _user_main_table_db_ tbl\n LEFT JOIN _user_joining_table_db_ jt\n ON jt.id = tbl.jt_id\n WHERE ...\n AND ...\n AND tbl.id IN (\n SELECT at.id\n FROM _user_another_table_db_ at\n WHERE at.active = 'Y'\n )\"";
$res = preg_match_all($re, $str, $match);
print_r($match[1]);
<强> Regex Demo 强>
答案 1 :(得分:0)
$pattern = '/_user_(.*)_db_/';
preg_match_all($pattern, $sql, $match);
var_dump($match[0]);