我現在遇到一個相當混亂的邏輯來查詢某種記錄。現在,我有一個表格,它們有各種記錄與它們的ProductID和UserEmail相關聯。我需要一個確切的sql語句來產生我需要的結果。我在這裏要做的是查詢或獲取與該UserEmail的產品ID相關聯的產品ID的記錄集合,並將該記錄與具有該特定UserEmail的記錄排除在外。例如,請參閱下面的表格,我想要獲取產品ID的所有記錄集,這些記錄具有UserEmail = [email protected]並排除具有該UserEmail的特定記錄。 我嘗試了一些使用連接的語句,並最終使用了這個語句,但仍然會返回空值。如何選擇具有關聯ID的記錄,但將結果排除在外?
SELECT DISTINCT ProductID,UserEmail,Comments,UserName FROM [ProdCommentsTab]
WHERE NOT EXISTS(SELECT ProductID,UserEmail,Comments,UserName FROM [ProdCommentsTab]
WHERE UserEmail = @useremail)
表
**ProductID** **UserEmail** **UserName** **Comments**
c764cbc3 [email protected] dfgfdhfdhfg fgshhfdfgh fhdfhhfgj
c764cbc3 [email protected] MyName dgsfdhfg fghdfjghj
c764cbc3 [email protected] dgdfgfhfhf dgsdgdf fghfdhfg
f08b9787 [email protected] dfgsdffhhf dfgsdhf fhfhfhf fhfhfh
f08b9787 [email protected] dgsdgdfhfgh dfgshf fhdfhg
f08b9787 [email protected] MyName sdsdgdsgf dfhfhfdg
f08b9787 [email protected] dgsdgfhfdg dgsdfhffh fghdjghj
b1d9dd41 [email protected] dsgdfgd sdgdsgfd fhfdhfg
b1d9dd41 [email protected] MyName dgsdhfdg fghdjgj
e4f9cvd21 [email protected] dgsdfhfdfg dfgshfg fggjgh fghgjg
e4f9cvd21 [email protected] dfgdshfhf dfgdhfg fghdfggjg
輸出結果應該是這樣的
**ProductID** **UserEmail** **UserName** **Comments**
c764cbc3 [email protected] dfgfdhfdhfg fgshhfdfgh fhdfhhfgj
c764cbc3 [email protected] dgdfgfhfhf dgsdgdf fghfdhfg
f08b9787 [email protected] dfgsdffhhf dfgsdhf fhfhfhf fhfhfh
f08b9787 [email protected] dgsdgdfhfgh dfgshf fhdfhg
f08b9787 [email protected] dgsdgfhfdg dgsdfhffh fghdjghj
b1d9dd41 [email protected] dsgdfgd sdgdsgfd fhfdhfg
'SELECT * FROM ProdCommentsTab其中useremail <> @ useremail' –
你真的使用兩個不同的數據庫管理系統(MySQL的*和* SQL服務器)來解決這個問題?一個應該就夠了:-)請改變你的標籤。 –
您向我們展示的示例sql查詢存在問題,如果不存在行結果「SELECT ... FROM [ProdCommentsTab] WHERE UserEmail = @useremail」,則說「select ... from ...」。我認爲這不是你解釋的問題。在哪裏設置@useremail? – Nico