简体   繁体   English

SQL SELECT 多次返回相同的项目

[英]SQL SELECT returns same item more than one time

I have the following SQL Command:我有以下 SQL 命令:

SELECT * 
FROM Notes 
INNER JOIN  AuthorizedPersons 
ON Notes.idPass = AuthorizedPersons.idPass 
AND AuthorizedPersons.Privileged = 0 
AND Notes.idUser =7

This returns the correct items!这将返回正确的项目! BUT returns the same item twice for each AuthorizedPerson that exists!但是对于每个存在的 AuthorizedPerson 返回相同的项目两次!

(Using DISTINCT does not solve the problem because items can have the same name.) (使用 DISTINCT 不能解决问题,因为项目可以具有相同的名称。)

Query Results:查询结果: 在此处输入图片说明

As you can see in the idPass 15 and 16 the description can be the same BUT idPass cannot since it's the primary key!正如您在 idPass 15 和 16 中所见,描述可以相同但 idPass 不能,因为它是主键!

The query returns 3 times the idPass 30...查询返回 3 倍的 idPass 30...

尝试使用 Where 而不是第一个 AND。

SELECT * FROM Notes INNER JOIN AuthorizedPersons ON Notes.idPass = AuthorizedPersons.idPass WHERE AuthorizedPersons.Privileged = 0 AND Notes.idUser =7

In the table AuthorizedPersons ,column starting with ' IdUs.. ' repeating multiple times against the same idPass .That is why you are getting multiple rows against same value of idpass .For avoiding the duplicate records, you can either use a ' DISTINCT ' keyword after excluding that particular column or you can choose any one of the record from that duplicated record by eliminating the others.AuthorizedPersons表中,以“ IdUs.. ”开头的列针对同一个idPass重复多次。这就是为什么您根据idpass相同值获得多行。为了避免重复记录,您可以使用“ DISTINCT ”关键字在排除该特定列之后,或者您可以通过消除其他记录从该重复记录中选择任何记录。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM