mysql php preg_replace,正则表达式

发布于 2024-10-27 07:38:11 字数 3067 浏览 0 评论 0原文

我有一些像这样的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)
        );

I have some sql like this:

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

There is a function using the following code to calculate the page index

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);

it doesn't work with my sql, so i did some testing

$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);

it outputs

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

it seems that "group by customer_id" is where causing the trouble.please help me modify

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

or suggest a way to get around it?mysql_num_rows?

The whole function is

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;
}

I prefer to not change anything outside this function, whenever possible..

ok...I have decided to use my own dodgy fix

        //$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)
        );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文