求一个匹配SQL语句的正则

king2world 2006-08-20 11:05:46
匹配所有的sql语句,要求提取出select,from,where三个部分,order和group部分不要
select a.*,b.* from aaa a left join bbb b on a.id=b.id where a.id=33 group by a.id order by b.id
类似这样的语句 ,要求提取出 a.*,b.* 和 aaa a left join bbb b on a.id=b.id where a.id=33
...全文
356 6 打赏 收藏 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
helloyou0 2006-08-20
  • 打赏
  • 举报
回复
需要处理子查询吗?
SysTem128 2006-08-20
  • 打赏
  • 举报
回复
<?php
$Str="select a.*,b.* from aaa a left join bbb b on a.id=b.id where x=\"xasdfas select saflsafj from asfsaf where x=\"xasdfas select saflsafj from asfsaf where x=\"xasdfas select saflsafj from asfsaf where safasf\"\" safasf\" a.id=33 group by a.id order by b.id ";
print preg_replace("/select(.+?)from(.+?)where(.*?)(order|group)/","Se: $1<br />Fro: $2<br /> Whe $3 <p />",$Str);
?>结果:
---------------
Se: a.*,b.*
Fro: aaa a left join bbb b on a.id=b.id
Whe x="xasdfas select saflsafj from asfsaf where x="xasdfas select saflsafj from asfsaf where x="xasdfas select saflsafj from asfsaf where safasf"" safasf" a.id=33

by b.id group by a.id
---------------
可包含无限子查询
换成
'order by b.id group by a.id'
'group by a.id order by b.id'
都没问题但~~
哪位高手给看看,我搞了半天'by b.id group by a.id'也没去掉.
king2world 2006-08-20
  • 打赏
  • 举报
回复
这样不行,详细说一下吧,现在语句的可能性有
select .... from .....
select .... from .... order by ...
select .... from .... group by ... order by ...
select .... from .... where ....
select .... from .... where .... order by ...
select .... from .... where .... group by ... order by ...
主要的就是这些了吧,就是order by ... 部分不要 group by ...部分不要 然后有where的,join的 都包含到第二部分里边 select的部分算第一部分
helloyou0 2006-08-20
  • 打赏
  • 举报
回复
嘿嘿,要考虑这种情况:
select * from aaa where x="xasdfas select saflsafj from asfsaf where safasf";
SysTem128 2006-08-20
  • 打赏
  • 举报
回复
<?php
$Str="select a.*,b.* from aaa a left join bbb b on a.id=b.id where a.id=33 group by a.id order by b.id";
echo preg_replace("/select (.+?) from (.+?) where (.+?) /i","$1<br />$2<br />$3<br />",$Str);
?>

没搞清你要干什么~~
乱写个先……
king2world 2006-08-20
  • 打赏
  • 举报
回复
不需要,子查询不用管

21,889

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧