**Two clarifications:**

* I'm going to actually be doing pagination with this, so ideally it can be one SQL statement...
* If this helps, what I'm trying to do is: Get all content for user, BUT if content item has criteria in content\_criteria table, check that user also has criteria in users\_criteria table, OTHERWISE, content is valid for user

So this might be hard to explain. Let's start with the main tables:

**content:**

```
| contentIdNum | contentStr | programIdNum |

```

A piece of content (unlike users) can only be associated with one program. 
A typical record would be like this. Both are associated with a program with id of 1.

```
| 1 | Zelda | 1 |
| 2 | Mario | 1 | 

```

**content\_criteria:**

```
| contentIdNum | criteriaIdNum |

```

A typical record would be like this: (this basically a table that associates content with criteria. Not all content is associated with criteria.)

```
| 1 | 5 |

```

\*\* user\_criteria: \*\*

```
| userIdNum | criteriaIdNum |

```

A typical record would be like this: (this basically a table that associates users with criteria. Not all content is associated with criteria.) Note that userIdNum 1 has the same criteria as contentIdNum 1

```
| 1 | 5 |

```

\*\* program\_users: \*\*

```
| userIdNum | criteriaIdNum |

```

A typical record would be like this (both user 1 and 2 are associated with program 1)

```
| 1 | 1 |
| 2 | 1 |

```

So here's what I need to do:

* Create a MySQL call that returns all pieces of content a user has access to
* By default, a user has access to all content associated with the programs he is assigned to
* If a piece of content has criteria associated with it in the content\_criteria table, ONLY users with that corresponding criteria in the user\_criteria table have access to that content
* For example, if I pass in userIdNum 1, I should get both pieces of content
* If I pass in userIdNum of 2, I should only get one piece of content (the one with contentIdNum of 2)

Obviously selecting content that matches a user's programs is easy (below is what I have). I'm trying to figure out an easy way to filter the content - can I do it in one go, or do I have to do a secondary pass and loop through the content via PHP?

```
SELECT DISTINCT (c.contentIdNum), c.*
FROM content c , program_users pu 
WHERE 1=1 
AND pu.userIdNum = 2 AND pu.programIdNum = c.programIdNum

```