2015-10-16 32 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

相关问题