Jump to content

[SOLVED] JOIN AND WHERE


scottybwoy

Recommended Posts

OK, I am familiar with JOIN AND WHERE, but not together.

 

I have two tables; one with product_ids and category_ids, and one with all the product details.

 

I want to select all product_ids for a specific category_id and filter the results based on data from the second table.  Here's MyPsudo statement that I need help with :

SELECT products.products_model, products.products_image, products.products_price
RIGHT JOIN ON products_to_categories.products_id = products.products_id
WHERE products_to_categories.categories_id AND WHERE products.products_quantity > 0 AND WHERE products.products_status = 1
ORDER BY products.products_ordered DESC LIMIT 1

 

Thats probably really basic, I'm only just beginning to do more complex queries so please bear with me.  What would be the correct syntax for that statement above, I hope it makes sense.  Cheers

Link to comment
Share on other sites

Let me step out on a limb here and say that a RIGHT JOIN is very seldom what you want to use for any sort of basic querying. They don't behave like people assume. You probably want to be using INNER JOIN instead. Try this:

SELECT p.products_model, p.products_image, p.products_price
FROM products p
INNER JOIN products_to_categories ptc ON ptc.products_id = p.products_id
WHERE ptc.categories_id = 'whatever'
AND p.products_quantity > 0
AND p.products_status = 1
ORDER BY p.products_ordered DESC
LIMIT 1

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.