SQL查询 - 查找不存在子帖子的父帖子

时间:2013-01-26 00:51:22

标签: mysql sql

我正在使用以下查询:

SELECT 
count(tblReplies.repDate) as comReplies,
tblReplies.repDate
FROM tblReplies, tblQuestions, tblUsers
WHERE
tblQuestions.queID = tblReplies.repQuestionID 
AND tblQuestions.queCompanyID = tblUsers.uCompanyID
AND tblUsers.uID = tblReplies.repUserID 
AND tblUsers.uCompanyID = $comID 
AND tblQuestions.queID = $queID 
GROUP BY tblReplies.repID 
ORDER BY tblReplies.repDate ASC

现在,我正在尝试修改查询以查找tblQuestions(queID)中没有回复的帖子(数据库中没有子项的问题tblReplies)。任何人都有一个想法,我完全迷失了,希望那里有一个忍者:)

谢谢,

2 个答案:

答案 0 :(得分:2)

select q.*
from tblQuestions q 
left outer join tblReplies r on q.queID = r.repQuestionID
where r.repQuestionID is null

答案 1 :(得分:1)

使用NOT EXISTS

SELECT q.* 
FROM   tblquestions q
WHERE  NOT EXISTS (SELECT 1 
                   FROM   tblreplies r
                   WHERE  r.repquestionid = q.queid)