类似于社区的类别,群组和帖子。我需要选择与用户相关的所有帖子。这些关系是需要在我所属的任何组中创建的所有帖子。我用查询子查询来完成它。
现在,我想以某种方式优化它,所以我只有一个没有子查询的查询。唯一的问题,我不知道如何。此时,我正在寻求帮助。
SELECT ... /* Lot of stuff. */
FROM `posts`
JOIN `groups`
ON (`groups`.`id` = `posts`.`group_id`)
JOIN `users`
ON (`users`.`id` = `posts`.`user_id`)
WHERE `groups`.`id` IN (SELECT `group_id` FROM `group_members` WHERE `user_id` = '33') /* My sub-query. ID '33' is variable.*/
AND `posts`.`post_id` = 0 /* For only first level posts. */
如果您需要表结构,请询问。非常感谢您的建议!
答案 0 :(得分:1)
SELECT ... /* Lot of stuff. */
FROM `posts`
JOIN `groups`
ON (`groups`.`id` = `posts`.`group_id`)
JOIN `users`
ON (`users`.`id` = `posts`.`user_id`)
JOIN `group_members`
ON (`groups`.`id` = `group_members`.`group_id`)
WHERE `group_members`.`user_id` = '33'
AND `posts`.`post_id` = 0 /* For only first level posts. */
应该做的伎俩。 至少,它应该等同于问题中的查询 - 如果它是一致的,我不知道。
答案 1 :(得分:0)
试试这个
SELECT ... /* Lot of stuff. */
FROM `posts`
JOIN `groups`
ON (`groups`.`id` = `posts`.`group_id`)
JOIN `users`
ON (`users`.`id` = `posts`.`user_id`)
inner join `group_members` on (`groups`.`id` = `group_members`.`group_id`)
Where `group_members`.`user_id` = '33' AND `posts`.`post_id` = 0