【发布时间】:2011-04-30 04:29:16
【问题描述】:
我有一个类似的 SQL 查询:
select column1 as a, DECODE(x, 1, x1, 2, x2) as column2,
DECODE(y, 1, y1, 2, y2) as column3 from test
我想选择以下逗号(用**,**表示):
select a as column1 **,** DECODE(x, 1, x1, 2, x2) as column2 **,**
DECODE(y, 1, y1, 2, y2) as column3 from test
我为此使用/(?!(.*\))),|,(?=.*\()/gs,但你怎么能在http://gskinner.com/RegExr/ 中看到第一个DECODE 中的逗号也被选中了。我需要该正则表达式来解析此代码中使用的选择中的列:
$select = substr($sql, 6, strpos($sql, "from") - 6);
$parts = preg_split('/(?!(.*\))),|,(?=.*\()/s', $select);
$columns = array();
foreach($parts as $p) {
@list($id, $alias) = preg_split('/( as )/', $p);
$columns[trim($id)] = trim($alias);
}
【问题讨论】:
-
嗯,SQL 是一种不规则语言,因为括号对可以任意嵌套。
-
我不知道你想问什么。也许您可以改写问题,和/或提供一些示例输出?