简体   繁体   English

连接 MySQL 中的两个表,结果集中只有一行,从第二个表中选择了几列

[英]Join two tables in MySQL, have just one row in the result set with few columns selected from second table

I am having a challenge to achieve the following我在实现以下目标方面面临挑战

Have 2 tables namely Transaction Table and Transaction_Event_LOG_INFO table有 2 个表,即 Transaction Table 和 Transaction_Event_LOG_INFO 表

Transaction Table Details事务表详细信息

SL SL TXN_ID TXN_ID TXN_FOR TXN_FOR TXN_TYPE TXN_TYPE
1 1 111 111 Shopping购物 Debit借方
2 2 112 112 Rent Debit借方

Transaction_Event_LOG_INFO table Transaction_Event_LOG_INFO 表

SL SL TXN_ID TXN_ID EVT_ID EVT_ID EVT_CODE EVT_CODE EVT_CREATED_DT EVT_CREATED_DT
1 1 111 111 200 200 100 100 11-12-2020 12:00:00 11-12-2020 12:00:00
1 1 111 111 201 201 101 101 11-12-2020 12:01:00 11-12-2020 12:01:00
1 1 111 111 202 202 102 102 11-12-2020 12:02:00 11-12-2020 12:02:00
1 1 111 111 203 203 103 103 11-12-2020 12:03:00 11-12-2020 12:03:00
1 1 111 111 204 204 104 104 11-12-2020 12:04:00 11-12-2020 12:04:00
1 1 112 112 205 205 100 100 11-12-2020 12:05:00 11-12-2020 12:05:00
1 1 112 112 206 206 101 101 11-12-2020 12:06:00 11-12-2020 12:06:00
1 1 112 112 207 207 102 102 11-12-2020 12:07:00 11-12-2020 12:07:00
1 1 112 112 208 208 103 103 11-12-2020 12:08:00 11-12-2020 12:08:00
1 1 112 112 209 209 104 104 11-12-2020 12:09:00 11-12-2020 12:09:00

I need to join the above two tables based on TXN_ID (which is unique) and have just one row for every TXN ID and having all columns from 1st table and just the EVT_CREATED_DT from 2nd table like where EVT_CODE=100 and 104我需要基于 TXN_ID (这是唯一的)加入上述两个表,并且每个 TXN ID 只有一行,并且具有来自第一个表的所有列以及来自第二个表的 EVT_CREATED_DT,例如 EVT_CODE=100 和 104

like below像下面

SL SL TXN_ID TXN_ID TXN_FOR TXN_FOR TXN_TYPE TXN_TYPE EVT_CREATED_DT_FOR_100 EVT_CREATED_DT_FOR_100 EVT_CREATED_DT_104 EVT_CREATED_DT_104
1 1 111 111 Shopping购物 Debit借方 11-12-2020 12:00:00 11-12-2020 12:00:00 11-12-2020 12:04:00 11-12-2020 12:04:00
2 2 112 112 Rent Debit借方 11-12-2020 12:05:00 11-12-2020 12:05:00 11-12-2020 12:09:00 11-12-2020 12:09:00

You can do cnoditional aggregation:您可以进行条件聚合:

select t.*, te.evt_created_dt_100, te.evt_created_dt_104
from transaction t
inner join (
    select txn_id
        max(case when evt_code = 100 then evt_created_dt end) as evt_created_dt_100,
        max(case when evt_code = 104 then evt_created_dt end) as evt_created_dt_104
    from transaction_event_log_info
    where evt_code in (100, 104)
    group by txn_id
) te on te.txn_id = t.txn_id

In very recent versions of MySQL, this would be more efficiently phrased with a lateral join:在 MySQL 的最新版本中,这将更有效地表达为横向连接:

select t.*, te.*
from transaction t
cross join lateral (
    select
        max(case when te.evt_code = 100 then te.evt_created_dt end) as evt_created_dt_100,
        max(case when te.evt_code = 104 then te.evt_created_dt end) as evt_created_dt_104
    from transaction_event_log_info te
    where te.evt_code in (100, 104) and te.txn_id = t.txn_id
) te

If you want to allow transactions that have none of the two events, you would change the inner join to a left join - and the cross join lateral (...) te to a left join lateral (...) te on true .如果您想允许没有这两个事件的事务,您可以将inner join更改为left join - 并将cross join lateral (...) te更改为left join lateral (...) te on true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM