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. When the status changes, a new entry is made in the xref_ticket_status
table to reflect the new 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.
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
.
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
:
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". 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.
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
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.