SQL - Updating table with informaton from another table - mysql

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
)

Related

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.

Reading a database diagram

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.

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.

MySQL Queries - Selecting from 2 tables and if exists in the other check field

I am trying to pull products from a table called products, I also have a table called product_ranges.
products
--------
id
name
model
product_ranges
--------------
id
product_id
other_id
SELECT p.id
FROM products As p
LEFT JOIN product_ranges As pr ON (pr.product_id = p.id AND pr.other_id = 16)
This will select all products and include the product_ranges table columns too if the product exists in it but if it does exist in this table and the other_id does not equal 16 I don't want the product to be in the returned results but if the product doesn't exist at all in the other table I want it in the results still.
I am sure I have done this years ago but can't think of the SQL for it - if anyone knows the right query I would be grateful, thanks.
Updated:
SELECT p.id
FROM products
LEFT JOIN product_ranges pr ON pr.product_id = p.id
WHERE (pr.product_id IS NULL OR pr.other_id = 16)

how do I fix this MySQL DELETE query?

I have three tables, products, shops, and sex. I wish to DELETE rows in the products table having a product_id such that there exists no equivalent product_id in the sex table.
Further, these rows in the products table must have a shop_id equal to the shop_id in the shops table for the row whose shops.shop value is 'www.shop.com'.
so far I have
DELETE FROM products USING shops WHERE
products.shop_id=shops.shop_id AND
shops.shop='www.shop.com' AND NOT EXISTS
(SELECT sex.product_id FROM sex WHERE
sex.product_id=products.product_id)
but it appears it is not acceptable to make reference to products in the subquery as I have done. (I get the error Unknown table 'products' in MULTI DELETE.) How do I fix my mistake?
You can use JOINs in your DELETE statement:
DELETE a
FROM products a
JOIN shops b ON a.shop_id = b.shop_id AND b.shop = 'www.shop.com'
LEFT JOIN sex c ON a.product_id = c.product_id
WHERE c.product_id IS NULL
This will DELETE only the products which have a corresponding row in the shops table with shop = www.shop.com, but only if that product also does not have a corresponding row in the sex table.