具有相同id的MySQL多行同时满足两个不同的条件

时间:2013-06-18 20:07:38

标签: mysql

我正在使用OTRS服务台票务管理系统。当我执行以下查询时:

SELECT  `ticket`.`id` ,  `ticket_history`.`ticket_id` ,  `ticket_history`.`id` ,
`ticket_history`.`name` ,  `ticket_history`.`create_time` 
FROM  `ticket_history` 
INNER JOIN  `ticket` ON  `ticket_history`.`ticket_id` =  `ticket`.`id` 
WHERE  `ticket_history`.`name` LIKE  '%Raw% %new%'
OR  `ticket_history`.`name` LIKE  '%Close'
ORDER BY  `ticket_history`.`ticket_id` ,  `ticket_history`.`id` ASC 

我得到以下输出:

+----+-----------+-----+-------------------------------------------+---------------------+
| id | ticket_id | id  |                   name                    |     create_time     |
+----+-----------+-----+-------------------------------------------+---------------------+
|  1 |         1 |  79 | %%Close                                   | 2013-06-10 11:50:33 |
|  2 |         2 |   2 | %%2013060810000011%%Raw%%3 normal%%new%%2 | 2013-06-08 21:59:02 |
|  3 |         3 |   5 | %%2013060810000021%%Raw%%3 normal%%new%%3 | 2013-06-08 21:59:03 |
|  3 |         3 |  22 | %%Close                                   | 2013-06-08 22:10:41 |
|  3 |         3 |  82 | %%Close                                   | 2013-06-10 11:50:49 |
|  4 |         4 |  88 | %%Close                                   | 2013-06-10 11:51:32 |
|  5 |         5 |  64 | %%2013060910000019%%Raw%%3 normal%%new%%5 | 2013-06-09 17:12:09 |
|  5 |         5 |  85 | %%Close                                   | 2013-06-10 11:51:10 |
|  6 |         6 |  92 | %%2013061010000016%%Raw%%3 normal%%new%%6 | 2013-06-10 12:00:24 |
|  7 |         7 |  95 | %%2013061010000025%%Raw%%3 normal%%new%%7 | 2013-06-10 13:05:05 |
|  8 |         8 |  98 | %%2013061110000014%%Raw%%3 normal%%new%%8 | 2013-06-11 19:05:06 |
|  8 |         8 | 109 | %%Close                                   | 2013-06-17 23:57:35 |
|  9 |         9 | 163 | %%2013061810000011%%Raw%%3 normal%%new%%9 | 2013-06-18 02:05:06 |
+----+-----------+-----+-------------------------------------------+---------------------+

我需要修改上面的查询,因此我只能列出名称中包含“%RAW %% new%”的每个ticket_id的行,同时ticket_id中有一行“%Close”。

换句话说,应该从上面的输出中显示三行ticket_id 3,两行ticket_id 5和两行ticket_id 8。

5 个答案:

答案 0 :(得分:2)

SELECT `ticket`.`id` ,  `ticket_history`.`ticket_id` ,  `ticket_history`.`id` ,`ticket_history`.`name` ,  `ticket_history`.`create_time`
FROM tickets t INNER JOIN ticket_history th
ON t.ticket_id = th.ticket_id
WHERE
EXISTS(SELECT 1 FROM ticket_history WHERE name LIKE '%Raw% %new%' AND ticket_id = t.ticket_id)
AND
EXISTS(SELECT 1 FROM ticket_history WHERE name LIKE '%Close' AND ticket_id = t.ticket_id)

答案 1 :(得分:1)

我现在无法对此进行测试,但我认为这可能会有效:

SELECT  `ticket`.`id` ,  `ticket_history`.`ticket_id` ,  `ticket_history`.`id` ,
`ticket_history`.`name` ,  `ticket_history`.`create_time` 
FROM  `ticket_history` 
INNER JOIN  `ticket` ON  `ticket_history`.`ticket_id` =  `ticket`.`id` 
WHERE  `ticket`.`id` IN (SELECT `ticket`.`id` FROM `ticket_history`
                       WHERE  `ticket_history`.`name` LIKE  '%Raw% %new%')
ORDER BY  `ticket_history`.`ticket_id` ,  `ticket_history`.`id` ASC 

答案 2 :(得分:1)

您无法使用简单的OR来检查结果集中不需要的内容。我的第一个猜测是相关的子查询。可能表现不佳,但它应该工作。也许是这样的事情(在我的头顶,完全未经测试):

SELECT ...
FROM ticket_history AS th INNER JOIN ticket ON ticket_history.ticket_id = ticket.id
WHERE ticket_history.name LIKE '%Raw% %new%' 
AND EXISTS (SELECT * FROM ticket_history WHERE name LIKE '%Close' AND ticket_id = th.ticket_id)

答案 3 :(得分:1)

那些不是重复的行。您要求提供符合条件的票证历史记录及其票证。第一个ID是票证ID。第二个ID是故障单历史记录ID。由于每个故障单都有多个故障单历史记录条目,因此您将获得多个记录。我可能会尝试(但尚未测试):

SELECT  `ticket`.`id` 
FROM  `ticket` 
INNER JOIN  `ticket_history` t1 ON  t1.`ticket_id` =  `ticket`.`id` AND t1.`name` LIKE  '%Raw% %new%' 
INNER JOIN `ticket_history` t2 ON t2.ticket_id = ticket.id AND  t2.`name` LIKE  '%Close' 
ORDER BY  ticket.id ASC 

我从select中删除了ticket_history字段,因为没有办法获取没有多条记录的字段,因为您实际上要求的是与两张故障单历史记录匹配的故障单记录。

答案 4 :(得分:1)

最后,这是我根据您的建议和其他几个互联网链接提供帮助的正确解决方案。感谢您的帮助。

SELECT `ticket`.`id`, `th`.`ticket_id`, `th`.`id`, `th`.`name`,  `th`.`create_time`    
FROM `ticket_history` th   JOIN `ticket` ON  `th`.`ticket_id` =  `ticket`.`id`   WHERE (
            `th`.`name` LIKE  '%Raw% %new%'
            AND EXISTS (SELECT * FROM ticket_history th1 WHERE th1.ticket_id = ticket.id AND th1.name LIKE '%Close')
           )
        OR (
            `th`.`name` LIKE  '%Close'
            AND EXISTS (SELECT * FROM ticket_history th2 WHERE th2.ticket_id = ticket.id AND th2.name LIKE '%Raw% %new%')
           )  ORDER BY th.ticket_id, th.id ASC;