简体   繁体   English

如何对多查询的结果排序?

[英]how to sort the result of multiquery?

I use mysqli_multi_query() function and get result of two query in such a way that all first query result is sort date wise first and second query after that. 我使用mysqli_multi_query()函数,并以这样的方式获取两个查询的结果:所有第一个查询结果都是按日期排序的第一和第二查询。

Here is my queries: 这是我的查询:

$query = "SELECT 
            `b`.`class_name`, 
            `c`.`main_code`,`c`.`account_title`, 
            `a`.`ledger_entry`, `a`.`balance` AS `debit`, `a`.`date`, `a`.`v.no`, `a`.`interactive_person` 
         FROM `vouchers` AS `a`,`classes` AS `b`, `data` AS `c` 
         WHERE `a`.`post_status`='yes' 
           AND `a`.`cancel_status`='off' 
           AND `a`.`type`='CP' 
           AND `a`.`class_id` = `b`.`class_id` 
           AND `a`.`account_id`=`c`.`account_code` 
           AND `a`.`date` BETWEEN '$fromDate' AND '$toDate';";
$query .= "SELECT 
             `b`.`class_name`, 
             `c`.`main_code`,`c`.`account_title`, 
             `a`.`ledger_entry`, sum(`a`.`balance`) AS `credit`, `a`.`date`, `a`.`v.no`, `a`.`interactive_person` 
           FROM `vouchers` AS `a`,`classes` AS `b`,`data` AS `c` 
           WHERE `a`.`post_status`='yes' 
             AND `a`.`cancel_status`='off' 
             AND `a`.`type`='CP' 
             AND `a`.`class_id` = `b`.`class_id` 
             AND `a`.`interactive_person`=`c`.`main_code` 
             AND `a`.`date` BETWEEN '$fromDate' AND '$toDate' GROUP BY `v.no`;";

These are two query and now let me show you the way I use multiquery function: 这是两个查询,现在让我向您展示我使用多查询功能的方式:

if (mysqli_multi_query($con, $query)) {
    do {
        // Store first result set
        if ($result = mysqli_store_result($con)) {
            // Fetch one and one row
            while ($row = mysqli_fetch_assoc($result)) {
                if ($y > 700) {
                    $y = 30;
                    $pdf->AddPage();
                    $x = 36;
                }
                $y = $y + 18;
                $x = 10;

                $debit = 0;
                $credit = $row['credit'];
                if($credit==""){$credit = 0;}
                $debit = $row['debit'];
                if($debit==""){$debit = 0;}

                $pdf->SetFont('Times', 'B', 12, '', true);
                $pdf->MultiCell(70, 18, $row['date'], 1, 'L', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
                $x = $x + 70;
                $pdf->SetFont('Times', '', 12, '', true);
                $pdf->MultiCell(50, 18, $row['v.no'], 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
                $x = $x + 50;
                $pdf->MultiCell(100, 18, $row['main_code'], 1, 'L', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
                $x = $x + 70;
                $pdf->MultiCell(100, 18, $row['account_title'], 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
                $x = $x + 100;
                $pdf->MultiCell(100, 18, $row['class_name'], 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
                $x = $x + 100;
                $pdf->MultiCell(120, 18,$credit , 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
                $x = $x + 120;
                $pdf->MultiCell(80, 18, $debit , 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);

            }
            // Free result set
            mysqli_free_result($result);
        }
    } while (mysqli_next_result($con));
}

Everything work perfectly and I get output in pdf as: 一切正常,我得到的pdf输出为:

在此处输入图片说明

So now what I want to do is sort the details I got order by "ref number" or "date" . 因此,现在我要做的是按“参考号”或“日期”对获得订单的详细信息进行排序。

How can I do it ? 我该怎么做 ? Can I do it with any php function ? 我可以使用任何php函数吗? or should I change the technique of fetching data from database ? 还是应该更改从数据库中获取数据的技术?

You can modify your query statements. 您可以修改查询语句。 Just append "ORDER BY ref_number ASC" at the end of each SQL query for example. 例如,只需在每个SQL查询的末尾附加“ ORDER BY ref_number ASC”。

ASC stands for ascending (small to big), DESC for descending (big to small) ASC代表升序(从小到大),DESC代表降序(从大到小)

Sort directly the array 直接排序数组

while ($row = mysqli_fetch_assoc($result)) {

    foreach ($row as $r) {
        $dates[] = $r['date'];
    }

    array_multisort($row, SORT_ASC, $dates);

......

}

after execution of query storing data in array and then sorted it and it work the way i want so the answer is 执行查询后将数据存储在数组中,然后对其进行排序,并且它以我想要的方式工作,所以答案是

$pdf->SetFont('Times', 'B', 12, '', true);

$y = $y + 20;
$pdf->MultiCell(200, 18, 'Cash Payment Vouchers', 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
$y = $y + 20;
$pdf->MultiCell(70, 18, 'Date', 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
$x = $x + 70;
$pdf->MultiCell(50, 18, 'Ref.No', 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
$x = $x + 50;
$pdf->MultiCell(70, 18, 'Code', 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
$x = $x + 70;
$pdf->MultiCell(100, 18, 'Account Title', 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
$x = $x + 100;
$pdf->MultiCell(100, 18, 'Class', 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
$x = $x + 100;
$pdf->MultiCell(120, 18, 'Credit', 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
$x = $x + 120;
$pdf->MultiCell(80, 18, 'Debit', 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
if (mysqli_multi_query($con, $query)) {
    do {
        // Store first result set
        if ($result = mysqli_store_result($con)) {
            // Fetch one and one row

            while ($row = mysqli_fetch_assoc($result)) {
                $a[] = $row;
                $vNo[] = $row['date'];

                array_multisort($a, SORT_ASC, $vNo);
            }
            // Free result set
            mysqli_free_result($result);
        }
    } while (mysqli_next_result($con));
    array_multisort($a, SORT_ASC, $vNo);
    foreach ($a as $ro) {
        if ($y > 700) {
        $y = 30;
        $pdf->AddPage();
        $x = 10;
    }
        $debit = 0;
        $credit = $ro['credit'];
        if ($credit == "") {
            $credit = 0;
        }
        $debit = $ro['debit'];
        if ($debit == "") {
            $debit = 0;
        }
        $y = $y + 18;
        $x = 10;
        $pdf->SetFont('Times', 'B', 12, '', true);
        $pdf->MultiCell(70, 18, $ro['date'], 1, 'L', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
        $x = $x + 70;
        $pdf->SetFont('Times', '', 12, '', true);
        $pdf->MultiCell(50, 18, $ro['v.no'], 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
        $x = $x + 50;
        $pdf->MultiCell(100, 18, $ro['main_code'], 1, 'L', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
        $x = $x + 70;
        $pdf->MultiCell(100, 18, $ro['account_title'], 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
        $x = $x + 100;
        $pdf->MultiCell(100, 18, $ro['class_name'], 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
        $x = $x + 100;
        $pdf->MultiCell(120, 18, $credit, 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
        $x = $x + 120;
        $pdf->MultiCell(80, 18, $debit, 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false);
        $sum += $credit;
    }
}
$y = $y + 20;
$pdf->MultiCell(390, 18, 'Total :', 1, 'R', true, 1, '10', $y, true, 0, false, true, 0, 'T', false);
$pdf->MultiCell(120, 18, $sum, 1, 'C', true, 1, '400', $y, true, 0, false, true, 0, 'T', false);
$pdf->MultiCell(80, 18, $sum, 1, 'C', true, 1, '520', $y, true, 0, false, true, 0, 'T', false);
unset($a);
unset($vNo);
$sum = 0;

} }

main code to solve my problem was 解决我的问题的主要代码是

if (mysqli_multi_query($con, $query)) {
    do {
        // Store first result set
        if ($result = mysqli_store_result($con)) {
            // Fetch one and one row

            while ($row = mysqli_fetch_assoc($result)) {
                $a[] = $row;
                $vNo[] = $row['date'];

                array_multisort($a, SORT_ASC, $vNo);
            }
            // Free result set
            mysqli_free_result($result);
        }
    } while (mysqli_next_result($con));

and then use foreach loop to get data from stored array named $a 然后使用foreach循环从名为$ a的存储数组中获取数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM