2015-10-16 72 views
0

我現在遇到一個相當混亂的邏輯來查詢某種記錄。現在,我有一個表格,它們有各種記錄與它們的ProductIDUserEmail相關聯。我需要一個確切的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 
+1

'SELECT * FROM ProdCommentsTab其中useremail <> @ useremail' –

+1

你真的使用兩個不同的數據庫管理系統(MySQL的*和* SQL服務器)來解決這個問題?一個應該就夠了:-)請改變你的標籤。 –

+0

您向我們展示的示例sql查詢存在問題,如果不存在行結果「SELECT ... FROM [ProdCommentsTab] WHERE UserEmail = @useremail」,則說「select ... from ...」。我認爲這不是你解釋的問題。在哪裏設置@useremail? – Nico

回答

2

首先一個EXISTS或NOT EXISTS子句應相關,即您應與該記錄您的外部查詢。 (或使用IN使它不相關。)您希望在這裏使用EXISTS,因爲您正在尋找該電子郵件存在的產品

然後,您必須在您的WHERE子句中用電子郵件本身排除記錄。

DISTINCT在這裏完全不必要。爲什麼你會在表中有重複記錄?

所以:

select productid, useremail, comments, username 
from prodcommentstab this 
where exists 
( 
    select * 
    from prodcommentstab other 
    where other.useremail = @useremail 
    and other.productid = this.productid 
) 
and useremail <> @useremail; 

SQL小提琴:http://www.sqlfiddle.com/#!3/f2644/2

,或者在:

select productid, useremail, comments, username 
from prodcommentstab 
where productid in 
( 
    select productid 
    from prodcommentstab 
    where useremail = @useremail 
) 
and useremail <> @useremail; 

SQL小提琴:http://www.sqlfiddle.com/#!3/f2644/3

現在,您的問題不再是標籤的MySQL,這裏是你讀ta的另一種選擇只有一次。它使用分析功能來檢查產品是否具有相關電子郵件的條目。由於分析函數不能用於WHERE原因,我們需要一個外部查詢來過濾結果。

select productid, useremail, comments, username 
from 
(
    select productid, useremail, comments, username, 
    max(case when useremail = @useremail then 1 else 0 end) 
    over (partition by productid) as has_useremail 
    from prodcommentstab 
) mydata 
where has_useremail = 1 
and useremail <> @useremail; 

SQL小提琴:http://www.sqlfiddle.com/#!3/f2644/4

通常是掃描表只是一次收集所有數據的查詢速度更快,所以你可以試試看。

+0

這不會產生任何結果。我結束了運行時錯誤。另一個人的答案是最近的一個,但它被刪除了。我應該也贊成他。謝謝你的努力。我只會上傳你的答案。 – timmack

+0

這種簡單查詢的運行時錯誤?那麼你的設置肯定有問題。你會得到什麼錯誤?你有索引productid和useremail,不是嗎?甚至可能是兩列的複合索引?這些查詢是直截了當的解決方案,應該非常快。 –

+0

我得到了一個類似索引的錯誤,就像這樣,但不能再記得確切的錯誤信息 – timmack

0

使用自我加入。首先查找與您的輸入電子郵件相關的所有行,一旦找到這些數據,請將其與同一產品上的同一張表加入,但使用不同的電子郵件ID。嘗試使用此:

select p1.ProductID,p2.UserEmail,p2.UserName,p2.Comments 
from ProdCommentsTab p1 
inner join ProdCommentsTab p2 on p1.ProductID = p2.ProductID and p2.UserEmail != p1.UserEmail 
where p1.UserEmail = '[email protected]' 
+0

這隻會給出像迭代循環結果一樣的重複結果 – timmack

相關問題