1

我是 regx 的新手...我想使用正则表达式从给定查询中获取子查询。

例如我有如下查询

Select * from (
   select * from Table_A where ID = 90 
   UNION 
   select * from Table_B where ID = 90
)  as SUBQUERY left join TABL_ABC abc ON (abc.id = SUBQUERY.id)

现在我希望我的正则表达式只匹配以下行:

select * from Table_A where ID = 90 
   UNION 
   select * from Table_B where ID = 90

请帮助我,提前谢谢你...

4

2 回答 2

0

如果它是一个没有额外大括号的简单子查询,你可以使用这个正则表达式

/\(\s*(select[^)]+)\)/i
于 2012-11-28T09:35:53.013 回答
0
<?php
$sql = 'Select * from ( select * from Table_A where ID = 90 UNION select * from Table_B where ID = 90 ) as SUBQUERY left join TABL_ABC abc ON (abc.id = SUBQUERY.id)';

if( preg_match('/\(\s*(select.+)\) as /iU', $sql, $matched ) ){
    $subquery = trim( $matched[1] );
    var_dump( $subquery );   
}
于 2012-11-28T09:36:16.633 回答