查询显示每行两次

时间:2012-12-30 16:01:54

标签: sql sql-server-2008

它显示每行两次...我写它来从不同的表中选择一些行,它显示我想要的但是它显示了两次,每次都是。为什么?

SELECT
   tbluser.fullname, tbluser.email, tblJobAdv.advtitle, tblPersonalInfo.country, 
   tblPersonalInfo.city, tblJobOffer.digitalsignature 
FROM
   tblUser 
LEFT JOIN
   tblPersonalInfo ON tblUser.userid = tblPersonalInfo.userid
LEFT JOIN
   tblApplication ON tblApplication.userid = tblUser.userid 
LEFT JOIN
   tblJobAdv ON tblJobAdv.advid = tblApplication.advid
LEFT JOIN
   tblJobOffer ON tblUser.userid = tblJobOffer.userid
WHERE
   tblJobAdv.userId IN (SELECT userid FROM tblUser WHERE email = 'h@y.com')

4 个答案:

答案 0 :(得分:3)

它显示重复记录,因为您正在加入不同的维度。如果我不得不猜测,根据表名,用户正在申请多个工作。

快速简便的解决方法是select distinct

实际上,您应检查基础表以确保其中没有重复项。我对Application表持怀疑态度。我认为给定用户可能有多个应用程序。

答案 1 :(得分:1)

使用'group by'或'distinct'会使您的查询变得缓慢且效率低下。 我想你最好从其他表中找到重复的行,比如tblJobAdv, 并且为这些表格提供更多'where condition'。

SELECT tbluser.fullname, tbluser.email, tblJobAdv.advtitle, tblPersonalInfo.country, 
   tblPersonalInfo.city, tblJobOffer.digitalsignature 
FROM
   tblUser 
LEFT JOIN
   tblPersonalInfo ON tblUser.userid = tblPersonalInfo.userid
LEFT JOIN
   tblApplication ON tblApplication.userid = tblUser.userid 
LEFT JOIN
   tblJobAdv 
   ON tblJobAdv.advid = tblApplication.advid
   AND tblJobAdv.isUsable = 'Usable' /* some more where condition example */
LEFT JOIN
   tblJobOffer ON tblUser.userid = tblJobOffer.userid
WHERE
   tblJobAdv.userId IN (SELECT userid FROM tblUser WHERE email = 'h@y.com')

答案 2 :(得分:0)

您只需对记录进行分组不同即可(删除重复记录),

Select distinct tbluser.fullname, tbluser.email, tblJobAdv.advtitle, tblPersonalInfo.country, 
tblPersonalInfo.city, tblJobOffer.digitalsignature 
from tblUser 
left join tblPersonalInfo
ON tblUser.userid = tblPersonalInfo.userid
left join tblApplication
On tblApplication.userid = tblUser.userid 
left join tblJobAdv 
On tblJobAdv.advid = tblApplication.advid
left join tblJobOffer
On tblUser.userid = tblJobOffer.userid
where tblJobAdv.userId IN (select userid from tblUser where email = 'h@y.com')

答案 3 :(得分:0)

请尝试选择distinct以避免重复记录