我正在使用以下SQL语法检索数据:
SELECT TOP 5 EventId, EventTime, DeviceName, Comment, Tenant, TenantName, Individual,
InetDb.dbo.Individuals.FirstName, InetDb.dbo.Individuals.LastName, InetDb.dbo.IndivImages.UserImage
FROM taclogdata.dbo.Event
LEFT JOIN InetDb.dbo.Tenants
ON taclogdata.dbo.Event.Tenant = InetDb.dbo.Tenants.TenantId
LEFT JOIN InetDb.dbo.Individuals
ON taclogdata.dbo.Event.Individual = InetDb.dbo.Individuals.IndivId
AND taclogdata.dbo.Event.Tenant = InetDb.dbo.Individuals.TenantNdx
LEFT JOIN InetDb.dbo.IndivImages
ON InetDb.dbo.Individuals.IndivId = InetDb.dbo.IndivImages.IndivNdx
AND InetDb.dbo.Individuals.TenantNdx = InetDb.dbo.IndivImages.TenantNdx
WHERE (taclogdata.dbo.Event.EventTime > DATEADD(hh, -3, GETDATE())AND taclogdata.dbo.Event.EventTime < GETDATE())
AND (taclogdata.dbo.Event.Comment='Reader entry' OR taclogdata.dbo.Event.Comment='Reader exit')
AND (taclogdata.dbo.Event.DeviceName = 'L9 1/4/1'
OR taclogdata.dbo.Event.DeviceName='L1 2/1/1-2 MainD'
OR taclogdata.dbo.Event.DeviceName='L1 2/1/3-4 MainD'
OR taclogdata.dbo.Event.DeviceName='L1 2/6/1-2 Stair'
OR taclogdata.dbo.Event.DeviceName='L1 2/2/1-2 FDT1')
ORDER BY taclogdata.dbo.Event.EventTime DESC
此代码工作正常,但我试图简化结果 我尝试通过将DeviceName值替换为例如,简化查询返回的内容。显示结果时 L1 2/1 / 3-4 MainD 到主门(不替换数据库中的实际数据)
我怎么能实现这个目标呢?
提前谢谢,
Ĵ
答案 0 :(得分:1)
试试这个使用替换功能
SELECT TOP 5 EventId, EventTime, replace(DeviceName,'L1 2/1/3-4 MainD','L1 2/1/3-4 Main Door') as DeviceName, Comment, Tenant, TenantName, Individual,
InetDb.dbo.Individuals.FirstName, InetDb.dbo.Individuals.LastName, InetDb.dbo.IndivImages.UserImage
FROM taclogdata.dbo.Event
LEFT JOIN InetDb.dbo.Tenants
ON taclogdata.dbo.Event.Tenant = InetDb.dbo.Tenants.TenantId
LEFT JOIN InetDb.dbo.Individuals
ON taclogdata.dbo.Event.Individual = InetDb.dbo.Individuals.IndivId
AND taclogdata.dbo.Event.Tenant = InetDb.dbo.Individuals.TenantNdx
LEFT JOIN InetDb.dbo.IndivImages
ON InetDb.dbo.Individuals.IndivId = InetDb.dbo.IndivImages.IndivNdx
AND InetDb.dbo.Individuals.TenantNdx = InetDb.dbo.IndivImages.TenantNdx
WHERE (taclogdata.dbo.Event.EventTime > DATEADD(hh, -3, GETDATE())AND taclogdata.dbo.Event.EventTime < GETDATE())
AND (taclogdata.dbo.Event.Comment='Reader entry' OR taclogdata.dbo.Event.Comment='Reader exit')
AND (taclogdata.dbo.Event.DeviceName = 'L9 1/4/1'
OR taclogdata.dbo.Event.DeviceName='L1 2/1/1-2 MainD'
OR taclogdata.dbo.Event.DeviceName='L1 2/1/3-4 MainD'
OR taclogdata.dbo.Event.DeviceName='L1 2/6/1-2 Stair'
OR taclogdata.dbo.Event.DeviceName='L1 2/2/1-2 FDT1')
ORDER BY taclogdata.dbo.Event.EventTime DESC
答案 1 :(得分:1)
我的建议:
如果您可以替换SELECT
中的数据,例如:
SELECT REPLACE(DeviceName,N'2/1/3-4 MainD',N'Main Door')
如果你有很多替换,我建议创建一个临时表,加入它并从临时表中取代。
答案 2 :(得分:1)
评论太长了。
您问题的具体答案是使用REPLACE()
或CASE
声明。但是,您还应该将WHERE
子句更改为使用IN
并使用表别名,以便代码更易于编写和阅读:
FROM taclogdata.dbo.Event e LEFT JOIN
InetDb.dbo.Tenants t
ON e.Tenant = t.TenantId LEFT JOIN
InetDb.dbo.Individuals i
ON e.Individual = i.IndivId AND e.Tenant = i.TenantNdx LEFT JOIN
InetDb.dbo.IndivImages ii
ON i.IndivId = ii.IndivNdx AND Ii.TenantNdx = ii.TenantNdx
WHERE (e.EventTime > DATEADD(hour, -3, GETDATE()) AND
e.EventTime < GETDATE()
) AND
e.Comment IN ('Reader entry', 'Reader exit') AND
e.DeviceName IN ('L9 1/4/1', 'L1 2/1/1-2 MainD', 'L1 2/1/3-4 MainD',
'L1 2/6/1-2 Stair', 'L1 2/2/1-2 FDT1'
)