-1

I have 5 tables:

  1. sites
    • id
    • site name
  2. categories
    • id
    • name
  3. products
    • id
    • name
    • and-some-more-columns
  4. x_products_site
    • product_id
    • site_id
  5. x_products_categories
    • product_id
    • category_id

Now, i want all data from a product of a category (let's say all from category with id 1) but only of a product is for a particular site.

My first try was

    SELECT p.* 
    FROM `products` p,
         `categories` c,
         `x_product_site` xps, 
         `x_product_category` xpc
    WHERE c.`id` = '%1\$s'
    AND c.`id` = xpc.`category_id`
    AND xpc.`product_id` = xps.`product_id`
    AND p.`id` = xpc.`product_id`
    ORDER BY p.`name` ASC

Obviously this is not the way to do it.

Can someone give me a right query with or without joins?

6
  • Why don't you use some ORM? Thiás will be piece of cake in ORM, and pain by hand.
    – MightyPork
    Jan 5, 2015 at 11:35
  • @MightyPork The world is covered in applications for which you can't "just use an ORM" - loads of existing applications which aren't going to be converted, for example. Jan 5, 2015 at 11:36
  • Use Joins, adding AND operator is not the solution. Jan 5, 2015 at 11:36
  • Have a look at the JOIN syntax in the MySQL manual. If these are all full joins (a table is required both sides) then just use INNER JOIN.
    – halfer
    Jan 5, 2015 at 11:37
  • You should improve the title of your question to reflect the particular sql problem that you are trying to resolve. Moreover, please improve the syntax of the following sentence coz I'm afraid it is not very clear: "Now, i want all data from a product of a category (let's say all from category with id 1) but only of a product is for a particular site." Jan 5, 2015 at 11:38

3 Answers 3

2

Try this:

If you have site_id then use below query:

SELECT P.id, P.name 
FROM products P 
INNER JOIN x_products_categories PC ON P.id = PC.product_id AND PC.category_id = 1
INNER JOIN x_products_site PS ON P.id = PS.product_id 
WHERE PS.site_id = 1
ORDER BY P.name;

If you have site_name then use below query:

SELECT P.id, P.name 
FROM products P 
INNER JOIN x_products_categories PC ON P.id = PC.product_id AND PC.category_id = 1
INNER JOIN x_products_site PS ON P.id = PS.product_id 
INNER JOIN sites S ON PS.site_id = S.id 
WHERE S.site_name LIKE '%site_name%'
ORDER BY P.name;
1
  • THANK YOU!!! Worked like a charm. I slowly get the JOIN thingy. Made it now like this (because i need to get it by Categorie name and not ID (my bad, didn't mentioned that and my example was wrong)) SELECT P.id, P.title FROM products P INNER JOIN x_product_category PC ON P.id = PC.product_id INNER JOIN x_product_site PS ON P.id = PS.product_id INNER JOIN categories C ON PC.category_id = C.id INNER JOIN sites S ON PS.site_id = S.id WHERE C.name LIKE '%zelfvertrouwen%' AND S.id = 2 ORDER BY P.title
    – D3F
    Jan 5, 2015 at 12:40
1
SELECT 
  p.* 
FROM
  products p
  , categories c
  , sites s
  , x_product_categories xpc
  , x_product_site xps 
WHERE xpc.category_id = c.id 
  AND xpc.product_id = p.id 
  AND xps.product_id = p.id 
  AND xps.site_id = s.id 
  AND s.sitename = "site1" 
  AND c.id = 1 ;
7
  • Please add comment for down vote, it will help user to find out his mistake. Jan 5, 2015 at 12:15
  • (-1): Badly formatted, and how can this even work without the joins?
    – musefan
    Jan 5, 2015 at 12:16
  • he is using "products p, categories c, sites s" which is same as CROSS JOIN. Jan 5, 2015 at 12:24
  • 1
    @yogeshprajapati: Yeah... to be honest I wasn't sure if it would work or not, that wasn't really part of the downvote reason (hence the questioning of it). I just don't think it's a good answer, because of formatting and it's just a "wall of code". I don't vote on answers solely on the correctness of them. I vote on the quality and usefulness too
    – musefan
    Jan 5, 2015 at 12:49
  • @musefan, I am new to stack overflow can you please suggest me how could I format the answer. But not a good idea due to format you can decrease a rank. Jan 6, 2015 at 7:07
0

There is no need to join tables. After all, you only want to select from products. Use IN clauses to see if a product is in the other tables. The table categories is not needed here at all. There is nothing in that table that gives us any information needed.

Let's say you want products for category 1 and site 5.

select *
from products
where product_id in (select pc.product_id from x_product_category pc where pc.id = 1)
and product_id in (select ps.product_id from x_product_site ps where ps.id = 5);

For completeness sake, you can do the same with EXISTS. (Well, after all, you want to know if a record with the product exists in the other tables.)

select *
from products p
where exists (select * from x_product_category pc where pc.product_id = p.id and pc.id = 1)
and exists (select * from x_product_site ps where ps.product_id = p.id and ps.id = 5);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.