我做什么:
我正在编写一个C#应用程序,它将帮助员工处理电子邮件。我有一个带邮件的数据库(SQL Server 2008 Express)。一个表包含邮件本身(每行包含msgId, sender, recipients, subject, body
等列),另一个表包含员工如何处理邮件的数据 - 只要电子邮件分配了一些属性,带有属性的新行就是保存到第二个表。
第二个表包含列:msgId, forTeam, notForTeam, processedByTeam
。只要员工将邮件标记为与其团队相关,就会在TEAMXX
列中保存一行forTeam
值。每当员工对电子邮件执行其他操作时,TEAMXX
列中会保存processedByTeam
值的行。
我试图将电子邮件显示在"未处理的"观点,即。邮件有以下价值:
forTeam: null
notForTeam: everything different than 'TEAM01'
processedByTeam: everything different than 'TEAM01'
我一直试图写出许多不同的问题,但它仍然没有按照我想要的方式运作,这让我疯狂。将不胜感激的正确查询。
实施例
包含邮件的表格(Mails
):
msgId sender subject body received (sortingTime)
53 x@x.com test test 2012-05-11 11:00
54 b@b.com test2 test2 2012-05-10 10:00
55 h@h.com blah blah 2012-05-11 12:00
56 t@t.com dfgg dfgg 2012-05-11 12:30
带作业的表格(MailAssignments
):
msgId forTeam notForTeam processedByTeam
54 null TEAM02 null - this means TEAM02 hid the mail from their view
54 TEAM01 null null - this means TEAM01 assigned the mail to them
54 null null TEAM01 - TEAM01 has processed the mail
53 TEAM01 null null - invisible to other teams
53 null TEAM01 null - invisible for TEAM01 (and also for others because of the above)
56 TEAM01 null null
56 null null TEAM01 - mail processed by TEAM01
现在,我希望我的C#应用程序有一个显示邮件的视图为"未处理",即:
因此,对于两个表中的当前数据,最后,TEAM01应该在他们的" Unprocessed"仅查看:
mail 55
答案 0 :(得分:2)
DECLARE @teamName varchar(50) = 'TEAM01'
SELECT *
FROM Mails M
LEFT OUTER JOIN MailAssignments MA ON M.msgId = MA.msgId
WHERE MA.msgId IS NULL OR
(
MA.forTeam IS NULL AND
MA.notForTeam <> @teamName AND
MA.processedByTeam <> @teamName
)
这会做你想要的吗?