[英]MySQL – left join on last record in the right table
I need a Query using LEFT JOIN where I can find the last record of table 2. For this, I'm looking for the " Id
" of Table 1 in Table 2 in the column " channel
". 我需要一个使用LEFT JOIN的查询,在这里可以找到表2的最后一条记录。为此,我正在表“表2”的“ channel
”列中查找表1的“ Id
”。
My current query must be extended for this. 我当前的查询必须对此进行扩展。 I tried to work with several approaches, but did not succeed. 我尝试了几种方法,但没有成功。 I hope you can help me. 我希望你能帮助我。
https://murrayhopkins.wordpress.com/2008/10/28/mysql-left-join-on-last-or-first-record-in-the-right-table/ https://murrayhopkins.wordpress.com/2008/10/28/mysql-left-join-on-last-or-first-record-in-the-right-table/
Table 1 表格1
id id1 id2 g_id1 g_id2 datum
1 1 2 x x timestamp
2 3 1 x x timestamp
3 3 2 x x timestamp
Table 2 表2
n_id channel absender nachricht datum
1 1 2 messagetext1 2019-09-22 19:30:31
2 1 1 messagetext2 2019-09-22 19:35:31
3 2 1 messagetext3 2019-09-22 19:40:31
3 2 3 messagetext4 2019-09-22 19:42:31
4 3 2 messagetext5 2019-09-22 20:40:31
Table 3 表3
id firmenname
1 companyname1
2 companyname2
3 companyname3
Output 输出量
id id1 id2 g_id1 g_id2 datum firmenname firmenname2 nachricht
1 1 2 x x timestamp companyname1 companyname2 messagetext2
2 2 1 x x timestamp companyname2 companyname1 messagetext4
3 3 2 x x timestamp companyname3 companyname2 messagetext5
My current query 我目前的查询
SELECT a.*, b.firmenname as firmenname1, c.firmenname as firmenname2 FROM nachrichtensystem a LEFT JOIN spieler b ON a.id1 = b.id LEFT JOIN spieler c ON a.id2 = c.id WHERE id1 = $sp_id OR id2 = $sp_id ORDER BY a.timestamp DESC LIMIT $id, 8
Declaration Table 1 id is the ChannelId. 声明表1 id是ChannelId。 I need the hole Table 1 and the last message from this channel in Table 2. This with the LIMIT and the companyname (firmenname) in Table 3. In my current query is missing only the part with the last message from table 2. The firmenname 1 and 2 comes from table 3 This i need with 我需要孔表1和表2中此通道的最后一条消息。在表3中具有LIMIT和公司名称(firmenname)。在当前查询中,仅缺少表2中最后一条消息的部分。 1和2来自表3我需要
ORDER BY timestamp DESC LIMIT $id, 8
You need a subquery that returns the last message for each channel and joined to table1 and then join table3 twice: 您需要一个子查询,该查询返回每个通道的最后一条消息,并联接到table1,然后联接table3两次:
select t1.*, t31.firmenname, t32.firmenname firmenname2, t2.nachricht
from table1 t1
left join (
select t.* from table2 t
where not exists (
select 1 from table2
where channel = t.channel and datum > t.datum
)
) t2 on t2.channel = t1.id
left join table3 t31 on t31.id = t1.id1
left join table3 t32 on t32.id = t1.id2
order by t1.id
See the demo . 参见演示 。
Results: 结果:
| id | id1 | id2 | g_id1 | g_id2 | datum | firmenname | firmenname2 | nachricht |
| --- | --- | --- | ----- | ----- | --------- | ------------ | ------------ | ------------ |
| 1 | 1 | 2 | x | x | timestamp | companyname1 | companyname2 | messagetext2 |
| 2 | 3 | 1 | x | x | timestamp | companyname3 | companyname1 | messagetext4 |
| 3 | 3 | 2 | x | x | timestamp | companyname3 | companyname2 | messagetext5 |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.