2017-05-24 17 views
0

3種類のトランザクションのデータを保持する3種類のトランザクションテーブルがあります。 これらのそれぞれからデータを表示する必要がありますが、問題は、これらの行を混在させて、日付に応じてソートする必要があることです。 たとえば、私はtransac1、transac2、transac1とユーザテーブルを持っています。 transac1は2つ以上のテーブルから選択してください。

id txn_id user_id field_a date 
1 223 1  23 12/12/12 
2 r23 1  33 12/12/12 

transac2ようなことtransac3が

id txn_id user_id field_c date 
1 g4t3 1  73  12/12/12 
2 d3ts 1  83  12/12/12 

ようになり、私はこれらのデータを取得するときにデータが

id row_id txn_id user_id field_a field_b field_c table_name date 
1 2 wr3  1  0  93  0  transac_2 11/12/12 
2 1 223  1  23  0  0  transac_1 12/12/12 
3 2 r23  1  33  0  0  transac_1 12/12/12 
4 1 nne  1  0  53  0  transac_2 12/12/12 
5 1 g4t3  1  0  0  73  transac_3 12/12/12 
6 2 dt3s  1  0  0  83  transac_3 12/12/12 
ようにする必要があり

id txn_id user_id field_b date 
1 nne 1  53  12/12/12 
2 wr3 1  93  11/12/12 

ようになります

daこの日付に基づいてソートする必要があります。テーブルは他のテーブルと別々に結合されます。 全身これをどうすればいいのか分かります。ちなみに、私はCodeigniterフレームワークを使用しています。可能であれば、CIのクエリービルダーで可能です。 おかげ

+0

使用 'union'または'組合ALL'。このリンクを参照してください。 > https://www.w3schools.com/sql/sql_union.asp –

+0

@chiragpatelええ、私もそれを私に示唆しましたが、私は実際にその使い方を知らないのです。あなたは私のための質問を書いてください。それは後で修正することが容易であるため、CIの質問作成者が可能です – prabhjot

+0

大丈夫、私はあなたに答えます。 –

答えて

2

は、このソリューションをお試しください:

このソリューションは、行番号WITH

SELECT @rownum := @rownum + 1 rownum , t.* 
FROM ( 
       select id,txn_id,user_id,field_a, 0 as field_b, 0 as field_c, 
        'transac_1' as table_name , date from transac_1 
       UNION ALL 
       select id,txn_id,user_id,0 as field_a, field_b, 0 as field_c, 
        'transac_2' as table_name , date from transac_2 
       UNION ALL 
       select id,txn_id,user_id,0 as field_a, 0 as field_b, field_c, 
        'transac_3' as table_name , date from transac_3 
    )t , (SELECT @rownum := 0) r Order by t.date desc 

のために、このソリューションは、行番号

WITHOUT ためです
SELECT t.* 
    FROM ( 
        select id,txn_id,user_id,field_a, 0 as field_b, 0 as field_c, 
         'transac_1' as table_name , date from transac_1 
        UNION ALL 
        select id,txn_id,user_id,0 as field_a, field_b, 0 as field_c, 
         'transac_2' as table_name , date from transac_2 
        UNION ALL 
        select id,txn_id,user_id,0 as field_a, 0 as field_b, field_c, 
         'transac_3' as table_name , date from transac_3 
     )t Order by t.date desc 

このソリューションがお役に立てば幸いです。あなたが他のものが欲しいのであれば、私に教えてください。

+0

私は試してみましょう – prabhjot

+0

私は行番号の答えを更新します。 –

+0

よく行番号は重要ではありませんが、それは全体として私のために働いたが、私は 'created'フィールドに従って新しいテーブルをどのように並べ替えるのだろうかと思っています – prabhjot

1
  • 新しいシーケンスを生成するための変数を作成します:
  • は、必ずではないunion(労働組合として 重複を破棄します)

    set @var:=; 
    select @var:[email protected]+1, * from ( 
    select id as row_id, ..... from table_1 
    union all 
    select id as row_id, ..... from table_2 
    ) as alais 
    
1

この

Rextester Sampleをお試しくださいunion allを使用してください

select @rownum:[email protected]+1 as id, t.* from 
(
select id as row_id,txn_id,user_id,field_a,0 as field_b,0 as field_c, 
'transac_1' as table_name,date 
from transac1 
union all 
select id as row_id,txn_id,user_id,0 as field_a,field_b,0 as field_c, 
'transac_2' as table_name,date 
from transac2 
union all 
select id as row_id,txn_id,user_id,0 as field_a,0 as field_b,field_c, 
'transac_3' as table_name,date 
from transac3 
) t , (SELECT @rownum:=0) r 
order by t.date; 

出力:

+----+--------+--------+---------+---------+---------+---------+------------+---------------------+ 
| id | row_id | txn_id | user_id | field_a | field_b | field_c | table_name |  date   | 
+----+--------+--------+---------+---------+---------+---------+------------+---------------------+ 
| 1 |  2 | wr3 |  1 |  0 |  93 |  0 | transac_2 | 12.11.2012 00:00:00 | 
| 2 |  2 | d3ts |  1 |  0 |  0 |  83 | transac_3 | 12.12.2012 00:00:00 | 
| 3 |  1 | nne |  1 |  0 |  53 |  0 | transac_2 | 12.12.2012 00:00:00 | 
| 4 |  1 | 223 |  1 |  23 |  0 |  0 | transac_1 | 12.12.2012 00:00:00 | 
| 5 |  1 | g4t3 |  1 |  0 |  0 |  73 | transac_3 | 12.12.2012 00:00:00 | 
| 6 |  2 | r23 |  1 |  33 |  0 |  0 | transac_1 | 12.12.2012 00:00:00 | 
+----+--------+--------+---------+---------+---------+---------+------------+---------------------+ 
関連する問題