Original Post
Hi all, I'm currently trying to write some forum software, and I've come across something that looks like it could be done a lot more cleanly and probably faster. The three relevant tables are forum_list, which contains a list of all the forums, the categories they're in, and the "group_view" column. It's either zero (All groups can view thew forum), or a group ID number. Next, there's the user_group_users table, which is just a list of user IDs (user_id column) and group IDs (group_id column), for determining which users are in which groups. Finally, there's the user_groups table, which has information about user groups such as the group name, ID (group_id column), and the group moderator (group_mod_id column). So, here's my query for selecting all forums in category 0, which user ID 999 is allowed to view: The two nested SELECTs seems pretty horrible to me, and that's the main thing that I think can be optimised out somehow, I just don't know how... I'm pretty much a SQL novice, the last time I played with non-basic SELECTs was the 2nd year of my degree, which was 3 years ago [smile] I'm using MySQL 5.0.21 if it matters.
SELECT * FROM forum_list WHERE category_id='0' AND (group_view='0' OR
group_view IN (SELECT group_id FROM user_group_users WHERE user_id='999') OR
group_view IN (SELECT group_id FROM user_groups WHERE group_mod_id='999')
);