![]() |
Help with Multiple Joins
Hi,
I am not very good building mysql queries using Joins. I hope someone can help me. I have a table 'products' with id, name and category_id fields. I also have a table 'categories' with id and name fields. I also have a table 'product_extra_categories' which allows a product to be included in a category other than its primary one. It has fields id, product_id and category_id. I need to display a list of products.id's and the categories.name where the products.category_id = the chosen category number or the product has the chosen category number as an extra category, ie product_extra_categories.product_id = products.id AND product_extra_categories.category_id = categories.id I have tried many things and keep stuffing it up. Sorry for my poor explanation. Thanks in advance. |
I don't think
Code:
AND product_extra_categories.category_id = categories.idCode:
WHERE products.id = categories.product_idSomething like this (you don't give us the column names so I guess): Code:
select |
Sunfighter: That won't work if you has some products and/or categories that do *NOT* have any matching product_extra_categories.
He needs a LEFT JOIN in there in that case. Code:
select |
Thanks for your help.
I still can't get it to work using the INNER JOIN and LEFT JOIN. To make it a bit clearer my tables are set up like this, I trimmed excess fields out. Code:
CREATE TABLE IF NOT EXISTS `products` (With the query, I specify the category id, using 3 in this example. At the moment I have the query as:- Code:
SELECT I hope this explains it better. |
Oh, I see! Very clever!
One comment to start with: The products_extra_categories.id field is completely unnecessary and might even be considered a mistake. A better way to declare that table would be: Code:
CREATE TABLE IF NOT EXISTS products_extra_categories (*************************************************** So... what you want is a tad more complex. And it depends on whether you will ever have more than one extra category for a given product. And it depends, then, on whether you want to get all the extra categories back in a single record or you want to see them one per record. First answer, which works for *EITHER* no more than one extra category per product *OR* if there are multiple extra categories then you want to see one per result record: Code:
SELECT *************************************************** This second answer is in case you want to get only one result record per product. Code:
SELECT By the way: A piece of advice (which you don't have to take, but think hard about it): To avoid confusion, I *NEVER* use a field name of just ID. And I *ALWAYS* try to have primary and foreign key field names the same. I also try not to just use "name" as a field name, so that I don't have to alias the field when I use "name" from two different tables. So I would do: Code:
CREATE TABLE IF NOT EXISTS products ( |
You *might* think about rebuilding that set of tables.
As given, there is nothing to prevent you from accidentally assigning an "extra category" to a product that is actually the same as the primary category. You might think about this design: Code:
CREATE TABLE IF NOT EXISTS products (And now the query (showing the second one) becomes: Code:
SELECT |
Quote:
And also appreciate the advice on the field names for id and name etc. I will start to use these more unique names in future. I tried both of your other queries and I am still not getting the full list. Sorry if my explanations are poor. All I really need to retrieve is a list of product id's. I am just grabbing the category name while I am getting them. So I know the category id that I want the list of products from (3 for this example), getting any products whose products.category_id = 3 is simple. But the products whose values are in the products_extra_categories will have a different category_id so I think the 'WHERE p.category_id = 3' is making it only return products with that category id. What I need to get is a list of product id's where products.category_id = 3 and include the product_id's of any entries in products_extra_categories where products_extra_categories.category_id = 3. I am confusing myself just trying to explain it. Maybe I have gone about the whole extra_category approach the wrong way. I know I can get them using 2 queries, one on products and the other on products_extra_categories but I want the result to be ordered by the product name. Really appreciate the help. |
Shoot, then you are making it *WAY* too hard!
Code:
SELECT |
Quote:
|
Something that looked so simple becomes a complicated operation and then thined out to become simple.
I just love watching the Supreme Master Coder at work. |
If you use Joomla or Wordpress then you can use xampp to make a database. It is very helpful for you.
Just create a table select a name, like product. You can type into table product id, product name and product extra categories id as you want. |
Quote:
|
| All times are GMT +1. The time now is 03:29 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.