php找到字符串中的最后一个括号并在其前面插入一个文本

时间:2014-01-21 11:48:54

标签: php mysql

有人可以帮助我吗?

我有一个简单查询和子查询的程序。在子查询中我想修改where和limit。如何在查询中找到最后一个“)”并在其前面加上“限制10”。

可以在程序中获得的子查询示例。

我有“$ Qstring = array_pop(explode(')',$ Qstring));”

 SELECT 
    QE.Maand,
    QE.Week,
    QE.`Opzegging direct`,
    QE.`Opzegging einddatum`,
    QE.Totaal,
    QE.`Nieuwe Leverancier`
    FROM
    ( select
    MONTHNAME(calldate) as Maand,
    WEEKOFYEAR(calldate) as Week,
    O1.description as "Omschrijving", 
    COUNT(CASE WHEN O1.description ="Opzegging per direct" THEN 1 END)        AS "Opzegging direct",    
    COUNT(CASE WHEN O1.description ="Opzegging per einddatum" THEN 1 END)        AS "Opzegging einddatum",  
    COUNT(CASE WHEN O1.description ="Opzegging per einddatum" OR O1.description ="Opzegging per direct" THEN 1 END) as Totaal,
    COUNT(CASE WHEN O1.description ="Behouden" THEN 1 END)        AS "Behouden",
    O2.description as "Nieuwe Leverancier" 

    FROM best.prj_004_table LEFT OUTER JOIN best.prj_004_options O1 ON best.prj_004_table.rs_retentieresult = O1.value
    LEFT OUTER JOIN best.prj_004_options O2 ON best.prj_004_table.rs_newsuplier = O2.value 

     O2.description is not NULL  AND year(calldate) = year(CURDATE()) AND MONTH(calldate) = MONTH(CURDATE())    GROUP BY O2.description ORDER BY WEEK(calldate)  ) QE

1 个答案:

答案 0 :(得分:0)

$sql = 'SELECT * FROM (SELECT col1, col2 FROM table JOIN table2 ON col3=col4)';
$limit = ' LIMIT 5';
$position = strripos($sql ')');
$end = substr($sql, $position);
$replace = substr_replace($sql, $limit, $position);
$replace .= $end;

有目的地有点冗长,但它确实起作用。

相关问题