0

I try to select all the items from stores table that can belong to multiple categories

This is my table structure:

stores table

id
name

categories table

id
name

store_category table

store_id
category_id

I've tried the folowing query but it doesn't work as i wanted:

SELECT * FROM (stores JOIN store_category ON stores.id = store_category.store_id) JOIN categories ON store_category.category_id = categories.id

What i want to do is like:

Select all stores that have category_id = php var $category_id set in store_category

*added as pseudocode

How can i do this?

PS: example of populated tables

`stores`

id  |  name
1   |  amazon
1   |  aliexpress


`categories`

id  | name
1   | smartphones
2   | fashion

`store_category`

store_id  |  category_id
1         |  1
1         |  2
2         |  1
2         |  2

From this table design i want to express that both stores are included in the two categories

6
  • Can you explain a bit more what you want? Commented Nov 10, 2015 at 20:03
  • I want to display all the stores from one category Commented Nov 10, 2015 at 20:07
  • group by blog.jooq.org/2014/12/04/… Commented Nov 10, 2015 at 20:08
  • 1
    Do you have any kind of php script at all? E.g. setting up a database connection and issuing some kind of sql query. Or do you want a complete script? Commented Nov 10, 2015 at 20:10
  • I have thr script & database connection. I just can't select the query based on that condition. Commented Nov 10, 2015 at 20:16

3 Answers 3

1

If you want to select all the stores that belong to the category with name 'some_category' then you can try this:

SELECT aa.id, aa.name
FROM stores AS aa
INNER JOIN store_category AS bb
ON aa.id = bb.store_id
INNER JOIN category AS cc
ON bb.category_id = cc.id
WHERE cc.name = 'some_category';
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This is what i was looking for.
1

In this Sample i get first all row with the category name and then i join the stores. So you not must query all stores with there categories

SELECT s.name
FROM store_category AS sc
LEFT JOIN stores AS s ON sc.store_id = c.id
WHERE sc.category_id = 2;

It not testet. please let me now if it works for you.

3 Comments

it doesn't work. Let me explain again: I want to select id & name from stores where category_id from store_category = $category_id php variable also asigned to store_id
please see my updated question with example of table design
i have edit my anwser. so it easy to get all stores where have a category. in my sample i use cat_id 2
0

You have to simply do this:

SELECT table1.*, table2.*, table3.* from table1, table2, table3
   where table1.id=table2.id and table2.id=table3.id and 
   table3.id=table1.id;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.