有没有办法拆分查询?

时间:2013-05-11 11:36:27

标签: php split

有没有办法拆除查询?我的查询如下:

SELECT C18_NLE00102.DETAIL
,C18_NLE00102.LIB_STATUS
,CLIENT_NLE00103.SIZO_CAMPAIGN_ID__C
,DATEPART( week,C18_NLE00102.DATE) as Week
,DATEPART( year,C18_NLE00102.DATE) as Year
 FROM 
C18_NLE001 C18_NLE00102 INNER JOIN CLIENT_NLE001 CLIENT_NLE00103
 ON C18_NLE00102.INDICE = CLIENT_NLE00103.INDICE

我需要输出:

Array (
    [0] => SELECT C18_NLE00102.DETAIL 
    [1] => C18_NLE00102.LIB_STATUS 
    [2] => CLIENT_NLE00103.SIZO_CAMPAIGN_ID__C 
    [3] => DATEPART(week,C18_NLE00102.DATE) as Week
    [4] => DATEPART( year,C18_NLE00102.DATE) as Year  
    [5] => FROM C18_NLE001 C18_NLE00102 INNER JOIN CLIENT_NLE001 CLIENT_NLE00103 ON C18_NLE00102.INDICE = CLIENT_NLE00103.INDICE
)

我的输出现在是:

Array (
    [0] => SELECT C18_NLE00102.DETAIL 
    [1] => C18_NLE00102.LIB_STATUS 
    [2] => CLIENT_NLE00103.SIZO_CAMPAIGN_ID__C 
    [3] => DATEPART( week 
    [4] => C18_NLE00102.DATE) as Week 
    [5] => DATEPART( year 
    [6] => C18_NLE00102.DATE) as Year FROM C18_NLE001 C18_NLE00102 INNER JOIN CLIENT_NLE001 CLIENT_NLE00103 ON C18_NLE00102.INDICE = CLIENT_NLE00103.INDICE 
) 

我正在使用:

$more_dwarf_array = split(', ?',$QuerySaved );
print_r($more_dwarf_array);

问题在于DATEPART。

1 个答案:

答案 0 :(得分:1)

此示例的工作代码:

$string = 'SELECT C18_NLE00102.DETAIL
,C18_NLE00102.LIB_STATUS
,CLIENT_NLE00103.SIZO_CAMPAIGN_ID__C
,DATEPART( week,C18_NLE00102.DATE) as Week
 ,DATEPART( year,C18_NLE00102.DATE) as Year
 FROM 
C18_NLE001 C18_NLE00102 INNER JOIN CLIENT_NLE001 CLIENT_NLE00103
 ON C18_NLE00102.INDICE = CLIENT_NLE00103.INDICE';

$array = preg_split('/(?<=^)\s*,|^(?=\s*FROM)/m', $string); // Split by ',' that's at the beginning of the line and also split if there is a FROM at the beginning
$array[count($array)-1] = str_replace(array("\r\n", "\r", "\n", "\t"), '', $array[count($array)-1]); // This is to remove all newlines/tabs in the FROM part
$array = array_map('trim', $array); // Remove spaces/newlines from the values of the array
print_r($array); // output

<强>输出:

Array
(
    [0] => SELECT C18_NLE00102.DETAIL
    [1] => C18_NLE00102.LIB_STATUS
    [2] => CLIENT_NLE00103.SIZO_CAMPAIGN_ID__C
    [3] => DATEPART( week,C18_NLE00102.DATE) as Week
    [4] => DATEPART( year,C18_NLE00102.DATE) as Year
    [5] => FROM C18_NLE001 C18_NLE00102 INNER JOIN CLIENT_NLE001 CLIENT_NLE00103 ON C18_NLE00102.INDICE = CLIENT_NLE00103.INDICE
)

Online demo