Using mySQL

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
blackrobot
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Fri Feb 22, 2008 9:28 pm

Using mySQL

Post by blackrobot » Fri Feb 22, 2008 9:41 pm

I'm a beginning Joomla coder and as you'll be able to tell, a beginning mySQL coder.

I'm coding a module that uses group jive, but that doesn't come into play much for this question -- it's more of a basic mySQL difficulty.

There are 3 tables in the database that I want to draw from: gj_category, gj_groups, and gj_users.

What I want to do is select the id from the row in gj_category where its category is equal to 'mycat'.
Then, I want to select the id's from the gj_groups table where the categories are equal to 'gj_category.id' at 'mycat'
Finally, I want to select the ids from the gj_users table where the user's group id's are equal to 'gj_groups.id' I got from the last statment

Currently, I'm using a few mySQL calls to do this in separate queries, and then looping the last one to get each user who has that specific group_id. I know that there is a way to use JOIN and AS statements in order to make it a more efficient query. I apologize if this is a tad bit confusing, thank you in advance for the help!

macbloke
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu Feb 21, 2008 10:23 am

Re: Using mySQL

Post by macbloke » Sat Feb 23, 2008 10:16 pm

blackrobot wrote:What I want to do is select the id from the row in gj_category where its category is equal to 'mycat'.
Then, I want to select the id's from the gj_groups table where the categories are equal to 'gj_category.id' at 'mycat'
Finally, I want to select the ids from the gj_users table where the user's group id's are equal to 'gj_groups.id' I got from the last statment
Without knowing your table structure, its hard to fathom exactly but something along the lines of:

Code: Select all

SELECT c.*, g.*, u.* FROM #__gj_category AS c LEFT JOIN #__gj_groups AS g ON g.categoryid = c.id LEFT JOIN #__gj_users AS u ON u.groupid = g.id WHERE c.categoryname = '$mycat'
should see you OK. It will probably need a bit of twiddling.

StuG

User avatar
Rogue4ngel
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 199
Joined: Sun Nov 26, 2006 10:46 pm
Location: New York

Re: Using mySQL

Post by Rogue4ngel » Sat Feb 23, 2008 11:20 pm

If you're using mysql 4.1 or greater, it also supports subselects.. which may suit the purpose as well.


Post Reply