从两个表中读取数据
Read data from two tables
考虑下面列出的表格
Table credit
id cr_amount created_date
1 1000 2011-07-01
2 2000 2011-07-08
3 6000 2011-07-09
Table debit
个条目如下。
id dr_amount created_date
1 3000 2011-07-09
需要按照 created date
的顺序从上面的表格中读取列 cr_amount
、dr_amount
和 created_date
,如下所示。
cr_amount dr_amount created_date
1000 NULL 2011-07-01
2000 NULL 2011-07-08
6000 NULL 2011-07-22
NULL 3000 2011-07-09
您可能需要将两列都放入 union all:
select cr_amount,Null as 'db_amount',created from table_credit
union all
select Null,db_amount,created from table_debit
order by created
考虑下面列出的表格
Table credit
id cr_amount created_date
1 1000 2011-07-01
2 2000 2011-07-08
3 6000 2011-07-09
Table debit
个条目如下。
id dr_amount created_date
1 3000 2011-07-09
需要按照 created date
的顺序从上面的表格中读取列 cr_amount
、dr_amount
和 created_date
,如下所示。
cr_amount dr_amount created_date
1000 NULL 2011-07-01
2000 NULL 2011-07-08
6000 NULL 2011-07-22
NULL 3000 2011-07-09
您可能需要将两列都放入 union all:
select cr_amount,Null as 'db_amount',created from table_credit
union all
select Null,db_amount,created from table_debit
order by created