Jump to content

[SOLVED] Filtering content using multiple tables


sonnyb

Recommended Posts

MySQL version: 5.0.41

 

I'm developing drop-down filters for a company's internal video library. The two variables to filter by are $client and $cat. They have tables as follows (simplified):

 

table - clips:
*-------------*--------*
| ID | Client | Title  |
*-------------*--------*
| 11 |   1    | clip_1 |
| 12 |   2    | clip_2 |
| 13 |   3    | clip_3 |
*-------------*--------*

table - clients:
*-------------*
| ID | Name   |
*-------------*
| 1  |client1 |
| 2  |client2 |
| 3  |client3 |
*-------------*

table - categories:
*-------------*
| ID | Name   |
*-------------*
| 3  |cat1    |
| 7  |cat2    |
| 6  |cat3    |
*-------------*

table - clips2cat:
*------------------*
| clipID | catID   |
*------------------*
|    11  |3        |
|    11  |6        |
|    11  |7        |
|    12  |7        |
|    13  |6        |
|    13  |3        |
*------------------*

 

So clips2cat has a row for each category assigned to a clip.

 

Say someone chooses 'client2' from the dropdown, and 'cat3' from the category dropdown (so $client=2 and $cat=6).

 

What kind of query do I need to run that is going to retrieve rows from the clips table, where clips are in category 6 (matched from clips2cat) and belong to clientid 2?

 

I've been trying to use Unions but I'm getting lost. Sorry if this doesn't make any sense at all.

Link to comment
Share on other sites

Nevermind, I figured it out:

 

SELECT clips.*,
       clients.*
FROM   clips,
       clients,
       clips2cat
WHERE  clips.client = clients.id
       AND clips2cat.clipid = clips.id
       AND clips2cat.catid = $cat
       AND clients.id = $client 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.