简体   繁体   English

以最新输入的价值加入

[英]Joining with the most recently-entered value

I have a table that shows work tickets, basically like this: 我有一张表,显示工作票,基本上是这样的:

TABLE "ticket"
ticket_id INT UNSIGNED AUTO_INCREMENT
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ticket_name VARCHAR(64)

Then I have a table showing the possible list of status codes for a ticket: 然后,我有一张表,显示票的可能状态码列表:

TABLE "status_ticket"
status_id INT UNSIGNED AUTO_INCREMENT
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
status_name VARCHAR(64)

And I have one more table that logs the history of the statuses that a ticket has or has had: 我还有一个表可以记录故障单所具有的状态的历史记录:

TABLE "xref_ticket_status"
xref_id INT UNSIGNED AUTO_INCREMENT
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ticket_id INT UNSIGNED NOT NULL
status_id INT UNSIGNED NOT NULL

When the ticket is created, an entry is made in the ticket table for the ticket, and then an entry is made in the xref_ticket_status table assigning a status to the ticket by linking the ticket table to the status_ticket table. 创建票证后,会在ticket表中为该票证创建一个条目,然后在xref_ticket_status表中创建一个条目,通过将ticket表链接到status_ticket表来为票证分配状态。 When the status changes, a new entry is made in the xref_ticket_status table to reflect the new status. 状态更改时,将在xref_ticket_status表中xref_ticket_status一个新条目以反映新状态。 This way, I have a history of each status that a ticket has had and when it was assigned. 这样,我可以了解故障单所具有的每个状态以及分配时间的历史记录。 The most recent entry for any given ticket_id in the xref_ticket_status table is the ticket's current status. xref_ticket_status表中任何给定的ticket_id最新条目是票证的当前状态。

I'm not sure how I would join these three tables together to get a ticket's current status. 我不确定如何将这三个表结合在一起以获取票证的当前状态。 Essentially, I want to join the ticket table with the xref_ticket_status table where ticket_id matches but for the newest created column in the xref_ticket_status . 本质上,我想将ticket表与xref_ticket_status表(其中ticket_id匹配,但针对ticket_id中最新created列)结合xref_ticket_status

Thanks! 谢谢!

This should do the trick: 这应该可以解决问题:

select ti.ticket_id,ti.ticket_name,
xr.created, st.status_name
from ticket ti
inner join xref_ticket_status xr
on(ti.ticket_id = xr.ticket_id)
inner join status_ticket st
on(xr.status_id = st.status_id)
group by ti.ticket
order by xr.xref_id desc;

===============Update================== ===============更新==================

select * from
(select ti.ticket_id,ti.ticket_name,
xr.created, st.status_name
from ticket ti
inner join xref_ticket_status xr
on(ti.ticket_id = xr.ticket_id)
inner join status_ticket st
on(xr.status_id = st.status_id)
group by ti.ticket
order by xr.xref_id desc)cs
order by cs.ticket_name desc; //or asc

The following gets the most recent status_id and ticket_id from xref_ticket_status : 下面获取最近status_idticket_idxref_ticket_status

select xts.*
from xref_ticket_status xts
where not exists (select 1
                  from xref_ticket_status xts2
                  where xts2.ticket_id = xts.ticket_id and
                        xts2.created > xts.created
                 );

The logic is: "Get me all rows from xref_ticket_status where there is no row with the same ticket id and a more recent created date". 逻辑是:“从xref_ticket_status获取所有行,其中没有具有相同票证ID和更新日期的行”。 The would be the last date for each ticket. 这将是每张票证的最后日期。 And note that you can also use xref_id for the comparison rather than the date. 并请注意,您也可以使用xref_id进行比较,而不是使用日期。

The rest is just joining the tables together and choosing your desired columns: 剩下的只是将表连接在一起,然后选择所需的列:

select t.*, ts.*, xts.created
from xref_ticket_status xts join
     ticket t
     on xts.ticket_id = t.ticket_id join
     ticket_status ts
     on xts.status_id = ts.status_id
where not exists (select 1
                  from xref_ticket_status xts2
                  where xts2.ticket_id = xts.ticket_id and
                        xts2.created > xts.created
                 );

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

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