我构建了以下查询,
SELECT
posts.id,
posts.useraid,
posts.useradn,
posts.title,
posts.createdate,
posts.forumid,
posts.type,
posts.totalreplys,
users.photo AS creatorphoto
FROM posts
JOIN users ON(users.id = posts.useraid)
WHERE posts.forumid IN(
SELECT subscribe.forumid AS fsub
FROM subscribe
WHERE subscribe.useraid = '$myid'
) AND
posts.forumid IN(
SELECT forums.id
FROM forums
WHERE forums.relatedto=fsub
)
AND posts.type='post'
ORDER BY posts.timee DESC LIMIT 20
代码完美无需此部分
AND
posts.forumid IN(
SELECT forums.id
FROM forums
WHERE forums.relatedto=fsub
)
但是,我需要查询来选择他订阅的所有论坛中的所有帖子,包括子论坛帖子。
答案 0 :(得分:0)
'fsub'在这里没有范围,在您的查询中此时甚至不存在:
posts.forumid IN(
SELECT forums.id
FROM forums
WHERE forums.relatedto=fsub
)
你需要使用我认为的UNION。
答案 1 :(得分:0)
除了使用子查询,为什么不将subscribe和forums表添加到主FROM条件?
如,
FROM posts
JOIN users ON(users.id = posts.useraid)
JOIN subscribe ON (posts.forumid = subscribe.forumid)
JOIN forums ON (forums.relatedto = posts.forumid)
答案 2 :(得分:0)
试试这个:
SELECT
posts.id,
posts.useraid,
posts.useradn,
posts.title,
posts.createdate,
posts.forumid,
posts.`type`,
posts.totalreplys,
users.photo AS creatorphoto
FROM posts
JOIN users
ON users.id = posts.useraid AND posts.`type`='post'
JOIN subscribe
ON subscribe.forumid = posts.forumid AND subscribe.useraid = '$myid'
JOIN forums
ON posts.forumid = forums.relatedto
ORDER BY posts.timee DESC LIMIT 20
答案 3 :(得分:0)
这是您的答案,您需要先使用子查询加入订阅和表单。
SELECT
posts.id,
posts.useraid,
posts.useradn,
posts.title,
posts.createdate,
posts.forumid,
posts.type,
posts.totalreplys,
users.photo AS creatorphoto
FROM posts
JOIN users ON(users.id = posts.useraid)
WHERE posts.forumid IN(
SELECT subscribe.forumid AS fsub
FROM subscribe ,forums
WHERE subscribe.useraid = '$myid'
and forums.relatedto = subscribe.forumid
and forums.id = subscribe.forumid
)
AND posts.type='post'
ORDER BY posts.timee DESC LIMIT 20