这样的递归如何做?

WBOY
풀어 주다: 2016-06-13 11:08:10
원래의
812명이 탐색했습니다.

这样的递归怎么做?!


select a,
(select b from c where ...) as d,
e
from f,(select j from h where ...) as i
where ....
로그인 후 복사

我要把外层select ... from ...中,select后面的内容替换掉,而保留from后面的内容。最后变成:

select count(*)
from f,(select j from h where ...) as i
where ....
로그인 후 복사

其实这相当于xml/html节点的替换,类似递归问题,想了很久也没想到解决方法。
------解决方案--------------------
正则....不行

如果你只是想得到返回的行数, 你总是可以这样做:
select count(*) from (

-- 你的sql --
select a,
(select b from c where ...) as d,
e
from f,(select j from h where ...) as i
where ....

) tmp

如果你非要严格做替换, 要做语法分析, 考虑到单双引号, 括号等等....
------解决方案--------------------
不知道你是要写SQL指令,还是要做字符串替换
如果是做字符串替换,可以这么写
$s = <<< TXT
select a,
(select b from c where ...) as d,
e
from f,(select j from h where ...) as i
where ....
TXT;

$ar = preg_split('/(\(?\bselect\b
------解决方案--------------------
\bfrom\b)/i', $s, -1, PREG_SPLIT_NO_EMPTY
------解决方案--------------------
PREG_SPLIT_DELIM_CAPTURE);

$n = 0;
$st = array();
for($i=0; $i $t = strtolower($ar[$i]);
if($t == 'select'
------解决方案--------------------
$t == '(select') {
$st[] = $i;
}
if($t == 'from') {
if(count($st) == 1) break;
array_pop($st);
}
}
for($i--; $i>$st[0]+1; $i--) unset($ar[$i]);
$ar[$st[0]+1] = " count(*)\n";
echo join('', $ar);
로그인 후 복사

select count(*)
from f,(select j from h where ...) as i
where ....

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!