Gossamer Forum
Quote Reply
Logic
Can anyone think of better logic for this query.

I have three tables, KBAuth, KBCategory and Groups.....the KBAuth table has the following columns:

kba_grp_id_fk
kba_cat_id_fk

....and basically specifies user restrictions to categories so if a row was:

1
1

....and I was in group one and tried to access the category with ID 1 it would block me.

KBCategory stores the data for each category such as the id, name etc. And finally the Groups table just stores the group id and name.

So, on the main page I want to select all categories with a parent id of NULL but _not_ list categories where the user is blocked. So I have this which works:

Code:
my $cond = WO::SQL::Condition->new('cat_parent_id', 'IS', \'NULL');
my $sth = $DB->table('KBCategory')->select($cond);
while (my $cat = $sth->fetchrow_hashref) {

# Hide the category if the user doesn't have access :)
next if ($DB->table('KBAuth')->count( { kba_cat_id_fk => $cat->{cat_id}, kba_grp_id_fk => $USER->{user_status} } ));

push @loop, $cat;
}

....as you can probably see, I'm selecting the categories and then doing a count on the KBAuth table to see if the category id is listed as being blocked for this user id.

The thing that is bothering me is that if I have 25 top level categories then the code as it is will be doing 25 count queries.

I was wondering if there was a way to make it so there were less queries?

Last edited by:

Paul: Oct 5, 2002, 8:05 AM
Quote Reply
Re: [Paul] Logic In reply to
If you are using a recent version of MySQL that supports sub-queries, you could trying using sub-queries...

Example:

SELECT CatID, GroupID
FROM Category, Group
WHERE CatID IN (SELECT CatID FROM InterSectionTable)
AND GroupID IN (SELECT GroupID FROM IntersectionTable)

Don't know if this helps, but sub-queries work well, and you could replace IN with EXIST as well, I think for MySQL, too.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Stealth] Logic In reply to
Thanks. If this was for myself I'd probably try that but it will be used by others who may not have the latest version of mysql so I may just have to stick with the count :(
Quote Reply
Re: [Stealth] Logic In reply to
I didn't think any version that you can download supports sub-selects yet? What version do you have?

Cheers,

Alex
--
Gossamer Threads Inc.