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
<?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 に答える
0

中括弧を追加しない単純なサブクエリの場合は、この正規表現をそのまま使用できます

/\(\s*(select[^)]+)\)/i
于 2012-11-28T09:35:53.013 に答える