MySQL 4.1.xを使用しています。
disbursement テーブル(出金)
d_id│amout│timestamp
────────────────
1 │ 1000│2009-11-01 11:05:01
2 │ 500│2009-11-01 16:15:02
3 │ 1100│2009-11-03 11:33:28
credit テーブル(入金)
c_id│amout│timestamp
────────────────
1 │ 1000│2009-10-05 10:50:21
2 │ 200│2009-10-31 17:12:42
3 │ 800│2009-11-01 08:14:12
4 │ 1300│2009-11-02 13:43:71
このように、入金テーブルと出金テーブルが別で管理されています。
これをJOINして、時間順に並べたいのですが、
時間のフィールドが別々になってしまうため、
JOIN後、どのようにORDER文を書けばいいのか分かりません。
詳しい方、よろしくお願いいたします。
欲しい結果のイメージ
入金 │ 出金 │ 時間(これで並び替え)
────────────────
1000 │ │ 2009-10-05 10:50:21
200 │ │ 2009-10-31 17:12:42
800 │ │ 2009-11-01 08:14:12
│ 1000│ 2009-11-01 11:05:01
│ 500│ 2009-11-01 16:15:02
1300 │ │ 2009-11-02 13:43:71
│ 1100│ 2009-11-03 11:33:28
未確認ですが、こんな感じでどうでしょうか。
select * from ( select amout as 入金, 0 as 出金, timestamp as 時間 from credit union all select 0 as 入金, amout as 出金, timestamp as 時間 from disbursement ) tbl order by 時間
または、文字列として・・・
select * from ( select cast(amout as char) as 入金, '' as 出金, timestamp as 時間 from credit union all select '' as 入金, cast(amout as char) as 出金, timestamp as 時間 from disbursement ) tbl order by 時間
次のような感じでいけるのではないでしょうか。
UNIONを使います。
select * from (
(select amount as 入金,null as 出金,myTimestamp from dbo.income)
union
(select null as 入金,amount as 出金, myTimestamp from dbo.payment)
) as A
order by A.myTimestamp;
ありがとうございます。
UNIONで、期待通り行きました。