我需要一个查询,从两个表中获取数据并按日期对它们进行排序。
表1:发票
<?php
$query = mysql_query("select * from invoice where customer = 95");
?>
ID Customer Amount Date
1 95 1500 01-Apr-2017
2 95 5500 09-Apr-2017
3 95 22000 10-Apr-2017
4 95 35000 11-Apr-2017
表2:付款
<?php
$query = mysql_query("select * from Payments where customer = 95");
?>
ID Customer Amount Date
1 95 10000 02-Apr-2017
2 95 11000 09-Apr-2017
3 95 22000 11-Apr-2017
4 95 1200 15-Apr-2017
我需要输出如下:
ID Date InvoiceDR InvoiceCR
1 01-Apr-2017 1500 -
2 02-Apr-2017 - 10000
3 09-Apr-2017 5500 -
4 09-Apr-2017 - 11000
答案 0 :(得分:0)
您可以使用&#34;按顺序排列&#34;以此目的。您可以按升序和降序设置顺序(ASC或DESC)。使用此查询。
select * from Invoice,Payments where '{condition}' order by Invoice.Date ASC , Payemnts.Date ASC
此查询将首先根据第一个表(Invoice)对数据进行排序,然后排序的数据将按第二个表(Payemnts)排序
答案 1 :(得分:-1)
$query = mysql_query("SELECT * FROM (
(SELECT invoice.id, NULL AS invoiceDR, invoice.InvoiceCR, invoice.Date FROM invoice)
UNION ALL
(SELECT NULL AS id, paymentd.InvoiceCR, NULL AS InvoiceDR, payments.Date FROM Payments)
) results ORDER BY Date ASC");
在查询结束时按日期排序 并根据您的需要更改日期顺序