php - 如何对多查询的结果进行排序?
问题描述
我使用mysqli_multi_query()
函数并以这样的方式获取两个查询的结果,即所有第一个查询结果都是按日期排序的第一个查询和之后的第二个查询。
这是我的查询:
$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`;";
这是两个查询,现在让我向您展示我使用多查询功能的方式:
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));
}
一切正常,我得到 pdf 格式的输出:
所以现在我想做的是按 "ref number" 或 "date" 对我得到的订单进行排序。
我该怎么做 ?我可以用任何 php 函数来做吗?或者我应该改变从数据库中获取数据的技术?
解决方案
您可以修改查询语句。ref_number
例如,只需在每个 SQL 查询的末尾附加“ORDER BY ASC”即可。
ASC 代表升序(从小到大),DESC 代表降序(从大到小)
推荐阅读
- java - Java MapStruct:映射器在映射单个元素之前清除目标集合
- wordpress - Ninja 使用 get_user_meta 将值渲染到隐藏字段
- spring-boot - Spring启动Kafka轮询直到队列为空并休眠
- python - python从子进程中获取grep -q的输出
- c++ - 通过包装器传递指向 MPI_Win_allocate_shared 的指针
- python - 适用于允许您指定应调试哪些方法的类的 Python 装饰器。我该如何实施?
- python - 日期时间不能低于 1000 年
- azure-active-directory - 有没有办法使用 Graph API 调用来检索 appRoleName?
- python - 从需要时间加载的网页中抓取数据的问题
- server - 在 NixOS 上安装 Zabbix 服务器