比较2个MySQL字段

时间:2011-06-27 12:55:40

标签: mysql comparison whmcs

我正在尝试使用我们的支持系统使用的API来运行PHP脚本,但我对MySQL的熟练程度不足以解决这个问题。

我需要做的是彻底查找并找到“操作”值为“新支持票证已打开”的所有条目,取“tid”并找到具有“新票证响应”操作的相应条目由XXX“其中XXX是除”用户“之外的任何内容。我遇到的问题是实际上可能有多个匹配该描述的条目,但我只想找到第一个。

我的想法是,我希望显示一个图表,总结我们最初响应支持服务单的平均时间。

enter image description here

这是SQL的开始...

SELECT b1.tid AS Tid, MIN(DATEDIFF(b2.date,b1.date)) AS ResponseTime
FROM billing b1 inner join billing b2 
ON b1.tid = b2.tid
WHERE b1.action='New Support Ticket Opened'
AND b2.action LIKE 'New Ticket Response made by%'
GROUP BY Tid

任何帮助都会很棒!谢谢。

3 个答案:

答案 0 :(得分:0)

您必须使用LIMIT子句:

SELECT b1.tid AS Tid, MIN(DATEDIFF(b2.date,b1.date)) AS ResponseTime
FROM billing b1 inner join billing b2 
ON b1.tid = b2.tid
WHERE b1.action='New Support Ticket Opened'
AND b2.action LIKE 'New Ticket Response made by%'
AND b2.action != 'New Ticket Response made by User'
GROUP BY Tid
LIMIT 1

答案 1 :(得分:0)

SELECT b1.tid AS Tid, MIN(DATEDIFF(b2.date,b1.date)) AS ResponseTime
FROM billing b1 inner join billing b2 
ON b1.tid = b2.tid
WHERE b1.action='New Support Ticket Opened'
AND b2.action LIKE 'New Ticket Response made by%'
AND b2.action != 'New Ticket Response made by User' //to filter "New Ticket Response made by User"
GROUP BY Tid
LIMIT 1

答案 2 :(得分:0)

  

取“tid”并找到相应的条目,其中包含“由XXX制作的新票证回应”的操作,其中XXX是除“用户”之外的任何内容

Simples:

SELECT b1.tid AS Tid, MIN(DATEDIFF(b2.date,b1.date)) AS ResponseTime
FROM billing b1 INNER join billing b2 
  ON b1.tid = b2.tid
WHERE b1.action='New Support Ticket Opened'
AND b2.action LIKE 'New Ticket Response made by%'
AND b2.action <> 'New Ticket Response made by User'
GROUP BY Tid

如果你想找到真正的用户.....

SELECT SUBSTR(b2.action, LENGTH('New Ticket Response made by ')) AS user,
  b1.tid AS Tid, MIN(DATEDIFF(b2.date,b1.date)) AS ResponseTime
....

(因为LENGTH(由'创建'新故障单响应)是不变的,你将获得更好的性能,用文字整数替换它)