Select from 3 tables with a single query in MySQL with PHP

Hello,

I have to select all items from a MySQL table using PHP but from categories that the user is allowed to see. I've tried having all the categories in an array and then imploding them while using the IN statement in the SQL query (tried like 10 different methods with INT and STRING arrays) with no luck. The problem is that I'm getting dizzy while trying to figure out a somewhat complex (at least for me) JOIN statement.

I have the following tables:

Sample tables
As you can see, it should be pretty easy except that a user may have more than one category assigned to it. How can I show Helen in this example all the products and/or categories she can see in a single query?

Thanks in advance.
LVL 6
Cesar AracenaPHP EnthusiastAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulEE Topic AdvisorCommented:
select
*
from users
inner join matrix on users.user_division = matrix.division
inner join items on matrix.category_available = items.category
where users.user_name = 'Helen'

Open in new window


You don't give us much detail on your needs, for example you could do this:

select
i.*
from items i
inner join matrix m on i.category = m.category_available
inner join users u on m.division = u.user_division
where u.user_name = 'Helen'

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cesar AracenaPHP EnthusiastAuthor Commented:
Thanks Paul!

I'm not on my working computer and that's why I don't have all the needs in hand but this sure will work! I'll try it tomorrow and come back with the result.

Thanks again!
Cesar AracenaPHP EnthusiastAuthor Commented:
Thanks Paul! It works perfectly. And it was easier than I thought ;)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.