我在做什么:
我正在编写一个 C# 应用程序,它将帮助员工处理电子邮件。我有一个包含邮件的数据库(MS SQL Server 2008 Express)。一个表包含邮件本身(每一行都有 msgId、发件人、收件人、主题、正文等列),另一个表包含员工如何处理邮件的数据 - 每当电子邮件分配了某些属性时,都会有一个新行与属性保存到第二个表。第二个表包含列:msgId、forTeam、notForTeam、processedByTeam。每当员工将消息标记为与其团队相关时,都会在 forTeam 列中保存一行带有 TEAMXX 值的行。每当员工对电子邮件执行其他操作时,都会将具有 TEAMXX 值的行保存在 processesByTeam 列中。
我正在尝试获取具有以下值的电子邮件:
forTeam:'TEAM01'
notForTeam:与'TEAM01'不同的
所有内容 processesByTeam:与'TEAM01'不同的所有内容
我使用 MailAssignments 表在 Mails 表上执行 LEFT JOIN,然后在 WHERE 之后放置上述条件。结果应该显示已分配给 TEAM01 但尚未由该团队的员工处理的消息。
问题:
我无法让 SQL 查询正常工作。条件的任何组合都将返回额外的行(例如,当我想省略这些时,使用属性 notForTeam='TEAM01'!)或给我一个错误,即日期时间由于某种原因格式不正确(我正在按接收时间对电子邮件进行排序)。
示例查询:
SELECT TOP 30 Mails.*
FROM Mails
LEFT JOIN MailAssignments
ON Mails.msgId = MailAssignments.msgId
WHERE
(MailAssignments.forTeam='TEAM01')
AND (MailAssignments.notForTeam<>'TEAM01' OR MailAssignments.notForTeam IS NULL)
AND (MailAssignments.processedByTeam<>'TEAM01' OR MailAssignments.processedByTeam IS NULL)
ORDER BY Mails.sortingTime DESC
即使我将 WHERE 之后(以及 ORDER BY 之前)的条件减少到(MailAssignments.notForTeam<>'TEAM01')
进行测试,由于某种原因,我仍然会收到错误消息。
我错过了什么或做错了什么?
更新:
似乎这(MailAssignments.notForTeam<>'TEAM01')
以某种方式导致了问题。其他一切正常(等号也可以:MailAssignments.notForTeam='TEAM01')但是当我添加notForTeam<>
条件时,我得到不正确的日期时间错误(WTF?:/)
System.Data.SqlTypes.SqlTypeException:SqlDateTime 溢出。必须介于 1753 年 1 月 1 日上午 12:00:00 和 9999 年 12 月 31 日晚上 11:59:59 之间。
更新 - 示例:
table with mails ("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
table with assignments ("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
55 TEAM02 null null - assigned to TEAM02 but not processed yet
Now, I want my C# app to have a view which will display mails as "This Team's", ie.:
1. mail 54 was first hidden for TEAM02 - should be invisible for them
2. mail 54 was assigned to TEAM01 - should be now visible for them in "This Team's" view
3. mail 54 was processed by TEAM01 - should be now invisible in "This Team's" view
4. mail 53 was assigned to TEAM01 - should visible in their "This Team's" view (and ONLY for them)
5. mail 53 was hidden for TEAM01 - should be invisible to TEAM01 (and for anyone else because of prev. action)
6. mail 56 was assigned to TEAM01 - should be now visible only to TEAM01 in "This Team's"
7. mail 56 was processed by TEAM01 - should be now invisible for TEAM01 in "This Team's"
So, with the present data in both tables, finally, TEAM02 for example should see in their "This Team's" view only:
mail 55