1

我有两张桌子:ticket & history_ticket

桌票:

ticket_id  | ticket_desc   
 1         |  software   
 2         |  hardware   
 3         |  other

表 history_ticket :

history_id | ticket_id | message  | status    
 1         |  1        |  text    |  process 
 2         |  2        |  text    |  solve   
 3         |  3        |  text    |  process
 4         |  3        |  text    |  solve

我想要这样的结果

ticket_id  | ticket_desc  | status 
 1         |  software    |  process
 2         |  hardware    |  solve
 3         |  other       |  solve

我尝试了各种连接和子选择,但没有运气

任何帮助/方向将不胜感激!

更新:如果我改变结果,像这样

ticket_id  | ticket_desc  | last_status | count_message
     1     |  software    |  process    |     1
     2     |  hardware    |  solve      |     1
     3     |  other       |  solve      |     2
4

4 回答 4

0
select distinct ticket.ticket_id, ticket.ticket_desc, history_ticket.status 
from ticket 
join history_ticket on ticket_id

据我所知,如果您有多个描述和状态,它将随机选择一个描述和状态。如果您想应用特定规则来选择哪个规则,请提供更多信息,我们可以在这方面为您提供帮助。

于 2013-02-19T19:30:08.930 回答
0

尝试

SELECT DISTINCT 
    tk.ticket_id, 
    tk.ticket_desc, 
    ht.status 
FROM ticket tk JOIN history_ticket ht ON tk.ticket_id = tk.ticket_id 
ORDER BY tk.ticket_id
于 2013-02-19T19:31:03.130 回答
0

尝试,

Select distinct t.ticket_id, t.ticket_desc, h.status 
from ticket t, history_ticket h
where t.ticket_id = h.ticket_id
order by t.ticket_id
于 2013-02-19T19:31:13.610 回答
0

试试这个:

SELECT
  t.ticket_id, 
  ticket_desc, 
  ht.status
FROM ticket AS t
INNER JOIN history_ticket AS ht ON t.ticket_id = ht.ticket_id
INNER JOIN
(
   SELECT ticket_id, MAX(history_id) maxid
   FROM history_ticket
   GROUP BY ticket_id
) AS ht2 ON ht.history_id = ht2.maxid;

SQL 小提琴演示

这会给你:

| TICKET_ID | TICKET_DESC |  STATUS |
-------------------------------------
|         1 |    software | process |
|         2 |    hardware |   solve |
|         3 |     Problem |   solve |

更新 1

要获取每张票的消息数,您可以COUNT(history_id) AS sum_message像这样简单地包含在子查询中:

SELECT
  t.ticket_id, 
  ticket_desc, 
  ht.status, 
  ht2.sum_message
FROM ticket AS t
INNER JOIN history_ticket ht ON t.ticket_id = ht.ticket_id
INNER JOIN
(
   SELECT 
     ticket_id, 
     MAX(history_id) maxid, 
     COUNT(history_id) AS sum_message
   FROM history_ticket
   GROUP BY ticket_id
) AS ht2 ON ht.history_id = ht2.maxid;

更新的 SQL Fiddle 演示

这会给你:

| TICKET_ID | TICKET_DESC |  STATUS | SUM_MESSAGE |
---------------------------------------------------
|         1 |    software | process |           1 |
|         2 |    hardware |   solve |           1 |
|         3 |     Problem |   solve |           2 |

更新 2

如果您想为 ids 选择名称divisi_id,对于简单值,您可以使用以下CASE表达式:

SELECT
  t.ticket_id, 
  ticket_desc, 
  CASE 
    WHEN t.divisi_id = 101 THEN 'Divisi A'
    WHEN t.divisi_id = 102 THEN 'Divisi B'
  END AS 'Divisi',
  ht.status, 
  ht2.sum_message
FROM ticket AS t
INNER JOIN history_ticket ht ON t.ticket_id = ht.hticket_id
INNER JOIN
(
   SELECT hticket_id, MAX(history_id) maxid, COUNT(history_id) AS sum_message
   FROM history_ticket
   GROUP BY hticket_id
) AS ht2 ON ht.history_id = ht2.maxid;

更新的 SQL Fiddle 演示

这会给你:

| TICKET_ID | TICKET_DESC |   DIVISI |  STATUS | SUM_MESSAGE |
--------------------------------------------------------------
|         1 |    software | Divisi A | process |           1 |
|         2 |    hardware | Divisi B |   solve |           1 |
|         3 |     Problem | Divisi A |   solve |           2 |

对于多个值,您可以将它们放在临时表中,也可以在子查询中选择它们并加入表以获取如下名称:

SELECT
  t.ticket_id, 
  ticket_desc, 
  d.Divisi,
  ht.status, 
  ht2.sum_message
FROM ticket AS t
INNER JOIN history_ticket ht ON t.ticket_id = ht.hticket_id
INNER JOIN
(
   SELECT hticket_id, MAX(history_id) maxid, COUNT(history_id) AS sum_message
   FROM history_ticket
   GROUP BY hticket_id
) AS ht2 ON ht.history_id = ht2.maxid
INNER JOIN
(
  SELECT 101 AS divisi_id, 'Divisi A' AS Divisi
  UNION ALL
  SELECT 102             , 'Divisi B'
  ... -- here you put other values or you can join a temp table instead
) AS D ON t.divisi_id = D.divisi_id;

更新的 SQL Fiddle 演示

于 2013-02-19T19:32:32.070 回答