我现在遇到一个相当混乱的逻辑来查询某种记录。现在,我有一个表格,它们有各种记录与它们的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