如何在mysql中按日期顺序获取两个表的详细信息

时间:2015-01-29 05:40:23

标签: mysql

我有两个名为wsbill和wspay的表。 wsbill用于存储结算明细,wspay用于存储付款详细信息。我想要查看这两个表的交易细节。

我的桌子的结构是,

 mysql> select * from wspay;
 +------------+-------+------+
 | WDATE      | NAME  | AMT  |
 +------------+-------+------+
 | 2015-01-28 | Bilal | 2000 |
 | 2015-01-30 | Bilal | 5000 |
 +------------+-------+------+

  mysql> select * from wsbill;
  +------------+---------+-------+------+--------+-------+
  | WDATE      | WSELLER | BILL  | LESS | REASON | FAMT  |
  +------------+---------+-------+------+--------+-------+
  | 2015-01-27 | Bilal   | 11000 | 1000 | test   | 10000 |
  | 2015-01-29 | Bilal   | 12000 | 1000 | test   | 11000 |
  +------------+---------+-------+------+--------+-------+

现在我想要输出,

  2015-01-27  Bilal    11000  1000  test    10000
  2015-01-28  Bilal  2000 
  2015-01-29  Bilal    12000  1000  test    11000
  2015-01-30  Bilal  5000

表示按日期排序。我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

试试这个:

select *
from (
     select wdate, name, amt, null as less, null as reason, null as famt
     from wspay
     union
     select wdate, wseller, bill, less, reason, famt
     from wsbill
     ) x
order by wdate

答案 1 :(得分:0)

   SELECT * from ( select * from table1
   union
   select * from table2 )order by WDATE DESC