0

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

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

4 回答 4

3

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

一个快速简单的解决方法是select distinct

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

于 2012-12-30T16:04:22.537 回答
1

使用 'group by' 或 'distinct' 会使您的查询缓慢且低效。我认为您最好从 tblJobAdv 等其他表中找到重复的行,并为这些表提供更多“位置条件”。

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')
于 2012-12-31T06:17:31.387 回答
0

您只需要对记录进行分组distinct就可以了(删除重复项),

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')
于 2012-12-30T16:04:07.183 回答
0

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

于 2012-12-30T16:04:44.360 回答