2014-01-21 54 views
-1

有人可以幫我嗎?php找到字符串中的最後一個括號並在其之前插入文本

我有一個簡單的查詢和子查詢程序。在子查詢中我想修改where和limit。如何在查詢中找到最後一個「)」,並將「限制10」放在它之前。

子查詢的例子,因爲它可以在程序中獲得。

我有 「$的QString = array_pop(爆炸( ')',$的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 
+2

您的PHP代碼與SQL可疑類似。你確定它是PHP嗎? –

+0

Ofcourse它是PHP!你能否看到括號打開(和關閉)? –

回答

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; 

有點冗長的目的,但它的工作。

+0

它的工作:) – Jacobs

相關問題