【JOINしてからの並び替え】


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

回答の条件
  • 1人2回まで
  • 登録:
  • 終了:2009/11/17 18:23:16
※ 有料アンケート・ポイント付き質問機能は2023年2月28日に終了しました。

ベストアンサー

id:HALSPECIAL No.2

回答回数407ベストアンサー獲得回数86

ポイント40pt

未確認ですが、こんな感じでどうでしょうか。

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 時間
id:keijiro

ありがとうございます!!

 

そうですね、UNIONでした。

うまく行きました。

2009/11/17 18:23:11

その他の回答2件)

id:aiaida333 No.1

回答回数166ベストアンサー獲得回数4

inner join でぐぐって見てください。

きっと、希望に合う答えが見つかります。

id:keijiro

ええと(汗

JOINは、分かるんですよ。

結合した後のソートです。

 

別々のテーブルに時間のフィールドがあって、それを元にソートしたいのです。

 

普通にJOINすると、

disbursementテーブルとcreditテーブルのtimestampフィールドが出てきちゃうので、

どちらかでしかソートできないと思います。

2009/11/17 17:30:37
id:HALSPECIAL No.2

回答回数407ベストアンサー獲得回数86ここでベストアンサー

ポイント40pt

未確認ですが、こんな感じでどうでしょうか。

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 時間
id:keijiro

ありがとうございます!!

 

そうですね、UNIONでした。

うまく行きました。

2009/11/17 18:23:11
id:b-wind No.3

回答回数3344ベストアンサー獲得回数440

ポイント40pt

んー、その例を見る限り必要なのは JOIN じゃなくて UNION の様に見えるが。

select d_amount AS 入金, c_amount AS 出金, times AS 時間
  FROM (
     SELECT amount AS d_amount, NULL AS c_amount, timestamp AS times
      FROM disbursement
   UNION
     SELECT NULL AS d_amount, amount AS c_amount, timestamp AS times
  ) AS c_d
ORDER BY 時間

JOIN するキーがわからんのよね。

SELECT d.amount AS 入金, c.amount AS 出金, IFNULL(d.timestamp,c,timestamp) AS 時間
FROM disbursement AS d CROSS JOIN credit as c
ORDER BY 時間

こんなんか?

id:keijiro

ありがとうございます!!

 

JOIN、JOINと思っていたのは、

本当は、会員情報やら、なんやらかんやら、JOINしたあとの結果テーブルから、

さらに、合体!という感じだったので、JOINと書いてしまいました。

 

ほんと、UNIONです(汗

タバコすって、コーヒー飲んで、落ち着いてから、作業します m(_ _)m

2009/11/17 18:14:38
  • id:freemann
    mySQLで試してないのでコメントで。
    次のような感じでいけるのではないでしょうか。
    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;
  • id:keijiro
    freemannさま
     
    ありがとうございます。
    UNIONで、期待通り行きました。

この質問への反応(ブックマークコメント)

「あの人に答えてほしい」「この質問はあの人が答えられそう」というときに、回答リクエストを送ってみてましょう。

これ以上回答リクエストを送信することはできません。制限について

回答リクエストを送信したユーザーはいません