添加列的mysql查询

wpx232ag  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(236)

很好的一天
希望你能帮助我,因为我无法用我的脑子来解决这个问题。:)
我有一个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抱怨这两个查询有不同的数字列。
如有任何帮助,我们将不胜感激。

sqxo8psd

sqxo8psd1#

您可以在下面尝试-只需在选择列表中添加计算字段

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)
jvidinwx

jvidinwx2#

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

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)

相关问题