Reading a database diagram - mysql

I've been trying to figure out how to get the products that match a certain category id but I have been unable to figure out how to move from category to products.
How would a query that basically selects all products that match a certain category id look?

This should work:
SELECT products.*
FROM products,
product_category
WHERE product_category.categoryid = CATEGORY_ID
AND products.catalogid = product_category.catalogid
Or if you prefer a join:
SELECT products.*
FROM products
INNER JOIN product_category ON products.catalogid = product_category.catalogid
WHERE product_category.categoryid = CATEGORY_ID
Simply replace CATEGORY_ID by the ID of the category you wish to select.
product_category is a link table, joining the tables products and product_category together: it contains the catalogid, referencing the ID of the category, and catalogid, referencing the ID of the product.

Related

Inner Join on many-to-many relationship on the same table

I came across a weird problem while I was trying to apply the Inner Join on the table which has 3 columns -
Table category_subcategory_relation
id (PK)
category_id_ref (FK)
subcategory_id_ref (FK)
The columns category_id_ref and subcategory_id_ref are the primary keys of the table category.
Table category
id (PK)
category_name (Varchar(200))
I want to Inner Join this two tables and get the columns as following -
Category_Name
Subcategory_Name
I am not able understand how would I be able to get the category_name column from the category table twice, once for the column Category_Name (FK) and again for the column Child_Category_Name (FK).
You would have to perform two joins, one each on category_id_ref and subcategory_id_ref:
SELECT a.*,
b.category_name AS Category_Name,
c.category_name AS Subcategory_Name
FROM category_subcategory_relation a
JOIN category b
ON a.category_id_ref = b.id
JOIN category c
ON a.subcategory_id_ref = c.id
While your category table contains category and subcategory information, you'll have to look at them as two separate tables (one for category and other for subcategory) to bring this information back to your category_subcategory_relation table.

How to get Data from three tables in one query where table 2 contains foreign keys from table 1 and 3

I want to get all the suppliers for one product with product details for which I am using following tables.
I have one Table products with columns
id(pk)
name
type
second table product_supplier with columns
psid(pk)
pid(fk from products)
sid(fk from supplier)
third table supplier with columns
id(pk)
firstname
lastname
I want to get data from these three tables in one mysql query.
Is this what you are looking for?
select p.*, s.*
from products p
inner join product_supplier ps on ps.pid = p.id
inner join supplier s on s.id = ps.sid
order by p.id, s.id
This will return each product along with all the associated suppliers.

How to associate sub-category to a field in database?

I am creating a MySQL database. I have a column named, name of the product. The product has one Main Category. However, it can belong to n-number of sub categories. I need to map these sub categories to that product. How can I do that?
Table1 - Product Info
Columns - ID, Name, MainCategory, SubCategory (Not Sure Exactly)
Table2 - MainCategory
Columns - ID, Name
Table3 - SubCategory
Columns - ID, Name
Table1 has 1-to-1 relationship to Table2.
How do I map Table1 to Table3? Am I doing this wrong?
Thought: I want to do it in the manner so that whenever I click on any subcategory name on a website, I get a list of all the products under that category. Just like it happens in Online Stores Website.
Example: The product External Hard Drive will come under Computer Accessories. But I want to give it sub-categories like offer_running, 500GB, SomeCompanyName, black etc.
Hope I explained my question. Please help me in the designing of database. I have got all the basics of DBMS, but I don't know how to involve keywords and to store & map them in a database.
How about the following structure:
Table1 - Product Info
Columns - ID, Name, Main_Category_ID
Table2 - Category
Columns - ID, Name
Table3 - Product_Category
Columns - ID, Product_ID, Category_ID, Name
Then you could use the following query to get all of the get all of the information:
SELECT p.*, c.Name
FROM [Product Info] p
INNER JOIN Product_Category pc ON p.ID = pc.Product_ID
INNER JOIN Category c ON pc.Category_ID = c.ID
Notice that in the Product Info table I added a Main_Category_ID field. If you really need to identify the main category then this would work:
SELECT p.ID, p.Name, NULL AS MainCat, c.Name AS SubCat
FROM [Product Info] p
INNER JOIN Product_Category pc ON p.ID = pc.Product_ID
INNER JOIN Category c ON pc.Category_ID = c.ID
UNION
SELECT p.ID, p.Name, c.Name AS MainCat, NULL AS SubCat
FROM [Product Info] p
INNER JOIN Category c ON p.Main_Category_ID = c.ID
First of all, if table 1 and table 2 have a 1-1 relationship, there is no need for having two separate tables. This will make query processing faster and will eliminate the need for an extra join.
Can you clarify what the ID column in table 3 refers to? Is this an unique ID for table 3 or the ID from table 1?
If former is the case, then you need to have the ID column of table 1 as the foreign key in table 3. That will resolve your issues.
Hope that helps.

SQL - Updating table with informaton from another table

I am trying to run an update on my companies eshop, but I'm racking my brain trying to work out the correct SQL command to do the following:
I have three tables:
Products
------------
pId
pSearchParams
ProductCategories
------------------
pcProductId // products.pId
pcCategoryId // categories.catId
Categories
----------
catId
catName
I need to update the pSearchParams field in the products table with the information from the catName field in the categories table.
The only way I can do this is to link the products table to the productcategories table, as the productcategories table is the only way I can establish a link between the products table and the categories table.
The problem is that in the productcategories table, pcProductId is likely to contain the same information on multiple rows, the same goes for pcCategoryId (e.g. products can be in multiple categories)
I need to run an UPDATE statement to update all the pSearchParams fields in the products table with the relevant category names, allowing for multiple category names.
If I need to provide more information please let me know. Thanks.
UPDATE products AS p
JOIN productcategories AS pc ON (p.pId = pc.pcProductId)
JOIN categories AS c ON (pc.pcCategoryId = c.catId)
SET p.pSearchParams = c.catName
Update
UPDATE products AS p
SET p.pSearchParams = (
SELECT GROUP_CONCAT(c.catName SEPARATOR ' ')
FROM productcategories AS pc
JOIN categories AS c ON (pc.pcCategoryId = c.catId)
WHERE pc.pcProductId = p.pId
GROUP BY pc.pcProductId
)

MYSQL Join if available

I have two tables I want to join, products and campaign_products
The tables look like this.
TABLE product
id | name
TABLE campaign_products
id | fk_campaign | fk_product | note
and if it's of interest
TABLE campaign
id | name
In all cases I need all the products returned, and if there is a row in the campaign_products which matches then add the values of those columns in as well. (null otherwise).
Remembering that multiple products can be part of multiple campaigns.
Here is what I tried, and in this case it only returns products that have a reference in campaign_products or products that don't exist at all in campaign_products so if the product is in campaign_products for another campaign it won't show up in other campaigns that don't include that product.
SELECT
product.id productId,
product.name productName,
campaign_product.note
campaignProductNote
FROMproduct
LEFT JOIN campaign_products ON product.id = campaign_products.fk_product
WHERE campaign_products.fk_campaign = 2 OR campaign_products.fk_campaign IS NULL
"2" changes based on the campaign being looked up.
I can't think of a way to solve this problem, is there a simple solution I'm missing ?
All you need to do is move the condition in the WHERE clause into the ON clause like so:
SELECT
product.id productId,
product.name productName,
campaign_product.note
campaignProductNote
FROM product
LEFT JOIN campaign_products ON product.id = campaign_products.fk_product
AND (campaign_products.fk_campaign = 2 OR campaign_products.fk_campaign IS NULL)