簡體   English   中英

mysql php preg_replace,正則表達式

[英]mysql php preg_replace, regular expression

我有一些這樣的SQL:

select o.customer_id, o.reference, o.firstname, o.lastname, os.name as status, o.date_modified, o.total as last_purchase_amount, sum(o.total) as total, count(o.order_id) as number_of_orders, o.currency, o.value from `order` o left join order_status os on o.order_status_id = os.order_status_id where os.language_id = '1' group by customer_id order by date_modified desc

有一個函數使用以下代碼來計算頁面索引

function splitQuery($sql, $page = '1', $max_rows = '20') {
        $count = $this->getRow(preg_replace(array('/select(.*)from /Asi', '/order by (.*)/i'), array('select count(*) as total from ', ''), $sql, 1));
        $pages = ceil($count['total'] / (int)$max_rows);

它不適用於我的SQL,所以我做了一些測試

$sql = "select o.customer_id, o.reference, o.firstname, o.lastname, os.name as status, o.date_modified, o.total as last_purchase_amount, sum(o.total) as total, count(o.order_id) as number_of_orders, o.currency, o.value from `order` o left join order_status os on o.order_status_id = os.order_status_id where os.language_id = '1' group by customer_id order by date_modified desc
";
echo preg_replace(array('/select(.*)from /Asi', '/order by (.*)/i'), array('select count(*) as total from ', ''), $sql, 1);

它輸出

select count(*) as total from `order` o left join order_status os on o.order_status_id = os.order_status_id where os.language_id = '1' group by customer_id

看來是“ group by customer_id”是造成麻煩的地方。請幫我修改

preg_replace(array('/select(.*)from /Asi', '/order by (.*)/i'), array('select count(*) as total from ', ''), $sql, 1)

或建議一種解決方法?mysql_num_rows?

整個功能是

function splitQuery($sql, $page = '1', $max_rows = '20') {
    $count = $this->getRow(preg_replace(array('/select(.*)from /Asi', '/order by (.*)/i'), array('select count(*) as total from ', ''), $sql, 1));

    $pages = ceil($count['total'] / (int)$max_rows);

    if (!$page) {
        $page = 1;
    }

    $offset = ((int)$max_rows * ($page - 1));

    $sql .= " limit " . (int)$offset . ", " . (int)$max_rows;

    $this->pages = (int)(($pages > 0) ? $pages : '1');
    $this->total = (int)$count['total']; 
    $this->from  = (int)(($offset > 0 || $count['total'] == 0) ? $offset+1 : '1');

    if ($count['total'] < $max_rows) {
        $this->to = (int)$count['total'];
    }  elseif ($this->pages == $page) {
        $this->to = (int)($offset + $max_rows - ($offset + $max_rows - $count['total']));
    } else {
        $this->to = (int)($offset + $max_rows);
    }

    return $sql;
}

我寧願在可能的情況下不要更改此功能之外的任何內容。

好的...我決定使用自己的躲閃修復程序

        //$count = $this->getRow(preg_replace(array('/select(.*)from /Asi', '/order by (.*)/i'), array('select count(*) as total from ', ''), $sql, 1));

        $this->result = mysql_query($sql);
        $count = array(
            'total' => mysql_num_rows($this->result)
        );

請在這里查看我的答案: 在對結果進行分頁時如何獲取查詢以轉移到后續頁面

分頁MySQL查詢結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM