要将列添加到的Mysql查询
你好 希望你能帮我,因为我的脑子还没弄明白 我有一个sql查询要将列添加到的Mysql查询,mysql,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 jo
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)
我试图做的是在输出中添加另一列,该列将以到期日后的天数显示请求的期限。我的查询本身就可以很好地完成这项工作
SELECT
DATEDIFF(CURDATE(),DATE(dCreated)) AS 'Ticket Age'
FROM _rtblIncidents
并计算天数
Ticket Age
63
37
28
21
17
17
我曾尝试合并查询,但没有成功。mysql抱怨这两个查询有不同的数字列
我们将不胜感激 您可以在下面尝试-只需在选择列表中添加计算字段即可
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)
您也可以使用子选择查询来计算字段
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)
不要使用单引号作为别名。单引号应仅用于字符串文字。
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)