我在做什麼有屬性的不同組合行:獲取從另一個表
我正在寫一個C#應用程序,這將幫助員工處理電子郵件。我有一個包含郵件的數據庫(MS SQL Server 2008 Express)。一個表格包含郵件本身(每一行都有msgId,發件人,收件人,主題,正文等欄目),另一個表格包含員工如何處理郵件的數據 - 只要電子郵件分配了一些屬性,新的一行與該屬性保存到第二個表。第二個表包含列:msgId,forTeam,notForTeam,processedByTeam。每當員工將消息標記爲與其團隊相關時,就會在forTeam列中以TEAMXX值保存一行。只要員工在電子郵件上執行其他操作,具有TEAMXX值的行就會保存在processedByTeam列中。
我試圖讓它們具有以下值的郵件:
forTeam: 'TEAM01'
notForTeam:一切比 'TEAM01' 不同
processedByTeam:一切比 'TEAM01'
跟我不一樣使用MailAssignments表在Mail郵件表上添加LEFT JOIN,然後在WHERE中添加上述條件。結果應該顯示已分配給TEAM01但尚未由該團隊的員工處理的消息。
問題:
我不能得到的SQL查詢才能正常工作。任何條件的組合都會返回額外的行(例如,當我想忽略這些行時,使用屬性notForTeam ='TEAM01')或給我一個錯誤,說明由於某種原因,datetime的格式不正確(我按收到的時間對郵件進行排序)。
例子查詢:
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')
進行測試,我仍然得到錯誤的某些原因。
我在想什麼或做錯了什麼?
UPDATE:
似乎(MailAssignments.notForTeam<>'TEAM01')
由於某種原因造成的問題。其他一切工作確定(也等號是OK:MailAssignments.notForTeam =「TEAM01」),但是當我添加了notForTeam<>
條件我得到不正確的日期時間錯誤(WTF:/)
System.Data.SqlTypes中.SqlTypeException:SqlDateTime溢出。必須介於1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM之間。
更新 - 例如:
table with mails ("Mails"):
msgId sender subject body received (sortingTime)
53 [email protected] test test 2012-05-11 11:00
54 [email protected] test2 test2 2012-05-10 10:00
55 [email protected] blah blah 2012-05-11 12:00
56 [email protected] 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
請不要用 「C#/ T-SQL」 前綴的稱號。這就是標籤的用途。 –
這是NotForTeam列的類型? – ivowiblo
此外,請嘗試在您的查詢分析器中運行查詢,並告訴是否發生錯誤 – ivowiblo