我有三种不同类型的事务表,它们保存3种不同类型的事务的数据。 我必须显示每个数据,但问题是我必须混合这些行并根据日期排序。 例如,我有transac1,transac2,transac1和用户表。 transac1就像
id txn_id user_id field_a date
1 223 1 23 12/12/12
2 r23 1 33 12/12/12
transac2就像
id txn_id user_id field_b date
1 nne 1 53 12/12/12
2 wr3 1 93 11/12/12
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
数据应根据此日期进行排序,表格将分别与其他表格连接。 任何身体知道我该怎么做。顺便说一句,我正在使用Codeigniter框架,如果它可能与CI的查询构建器,它将不胜感激。 感谢
答案 0 :(得分:1)
试试这个解决方案:
此解决方案适用于 WITH ROW NUMBER
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 ROW NUMBER
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
我希望这个解决方案对您有所帮助。如果您需要其他任何内容,请告诉我,否则此解决方案对您无效。
答案 1 :(得分:1)
确保使用union all
而非union
(因为工会将丢弃
一式两份)
set @var:=;
select @var:=@var+1, * from (
select id as row_id, ..... from table_1
union all
select id as row_id, ..... from table_2
) as alais
答案 2 :(得分:1)
试试这个
select @rownum:=@rownum+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 |
+----+--------+--------+---------+---------+---------+---------+------------+---------------------+