MySQL查询添加列

时间:2018-12-07 06:37:06

标签: mysql

早安

希望您能提供帮助,因为我无法用菜鸟的大脑解决这个问题。 :)

我有一个SQL查询。

SELECT 
  i.cOurRef as 'IR Number',
  date(i.dCreated) as 'Date Created',
  a.cDisplayName as 'Logged To',
  c.Name as Client,
  i.cOutline as Description,
  date(i.dDueBy) as 'Due date'

FROM
_rtblIncidents i

left join 
_rtblagents a on
    i.iCurrentAgentID = a.idAgents

left join client c on 
    i.iDebtorID = c.DCLink

where iIncidentStatusID <> '3' and iIncidentTypeID in (11,75) and 
  iCurrentAgentID in (285,284,266,55,113,282,190,293)

此部分正常工作。并产生所需的输出。

IR Number   Date Created    Logged To   Client  Description Due date
IR00032     2018/11/20      Eng 1       client 1    Desc 1  2018/12/07
IR00033     2018/12/06      Eng 2       client 2    Desc 2  2018/12/07
IR00034     2018/12/06      Eng 3       client 3    Desc 3  2018/12/07
IR00035     2018/12/05      Eng 4       client 4    Desc 4  2018/12/06
IR00036     2018/12/03      Eng 5       client 5    Desc 5  2018/12/07
IR00037     2018/11/26      Eng 6       client 6    Desc 6  2018/12/05

我想做的是在输出中添加另一列,该列将显示“到期日后几天内的请求的年龄。我自己执行此查​​询的查询很好。

SELECT 

DATEDIFF(CURDATE(),DATE(dCreated)) AS 'Ticket Age'

FROM _rtblIncidents

并计算日期。

Ticket Age
   63
   37
   28
   21
   17
   17

我尝试合并查询,但是不起作用,mysql抱怨2个查询具有不同的数字列。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

您可以尝试以下操作-只需将计算出的文件添加到选择列表中即可

SELECT 
  i.cOurRef as 'IR Number',
  date(i.dCreated) as 'Date Created',
  a.cDisplayName as 'Logged To',
  c.Name as Client,
  i.cOutline as Description,
  date(i.dDueBy) as 'Due date', DATEDIFF(CURDATE(),date(i.dCreated)) AS 'Ticket Age'

FROM
_rtblIncidents i

left join 
_rtblagents a on
    i.iCurrentAgentID = a.idAgents

left join client c on 
    i.iDebtorID = c.DCLink

where iIncidentStatusID <> '3' and iIncidentTypeID in (11,75) and 
  iCurrentAgentID in (285,284,266,55,113,282,190,293)

答案 1 :(得分:-1)

您也可以使用子选择查询来计算字段。

SELECT 
i.cOurRef as 'IR Number',
date(i.dCreated) as 'Date Created',
a.cDisplayName as 'Logged To',
c.Name as Client,
i.cOutline as Description,
date(i.dDueBy) as 'Due date',
(SELECT DATEDIFF(CURDATE(),DATE(i.dCreated))) AS 'Ticket Age'

FROM
_rtblIncidents i

left join 
_rtblagents a on
i.iCurrentAgentID = a.idAgents

left join client c on 
i.iDebtorID = c.DCLink

where iIncidentStatusID <> '3' and iIncidentTypeID in (11,75) and 
iCurrentAgentID in (285,284,266,55,113,282,190,293)