I have a main product table with different products. Different products have different specs, so I've created separate specs tables for each product (there will be more than ten of them). What I want to do is to show individual product's specs on a product_page.php whenever the product is clicked.
My product page has columns:
id - SKU - prod_name - prod_desc....
My specs table columns
id - SKU - prod_specs....
So I want to take SKU from first table and search the rest of the table with this UNIQUE sku and wherever it is show the rest of the field from that table
What I do is
SELECT SKU FROM products AS p
INNER JOIN cpu_specs AS cs ON cs.SKU = p.SKU
INNER JOIN hdd_specs AS hs ON hs.SKU = p.SKU
WHERE p.SKU = $productSKU "
But it gives me an error.
If I do SELECT * then it fetches all the info from both tables
Try this:
SELECT p.id, p.prod_name, p.SKU, IFNULL(cs.prod_desc, hs.prod_desc)
FROM products AS p
LEFT JOIN cpu_specs AS cs ON cs.SKU = p.SKU
LEFT JOIN hdd_specs AS hs ON hs.SKU = p.SKU
WHERE p.SKU = $productSKU
I didn't find any proper SELECT query that would find just one result from the tables, but came up with a workaround
In my table products each product has a subcategory (sub_cat),which is cpu, hdd etc.
So I've named the secification tables for each product like so hdd_specs, cpu_specs and so on.
So I store the sub_cat as a variable and then SELECT everything from the table called like my variable and it works smoothly.
$query = "SELECT sub_cat FROM products WHERE SKU = $productSKU";
$select = mysql_query($query);
$result_1 = mysql_fetch_assoc($select);
$sub_cat = $result_1['sub_cat'];
$sub_cat = $sub_cat.'_specs';
$get = " SELECT * FROM $sub_cat
WHERE SKU = $productSKU
";
Related
I am trying to get the category name from categories linkt with a retailer_id and category_id
These are the three tables:
1)retailers,
retailer_id
2)retailer_to_category,
retailer_id and
category_id
3)categories,
category_id and
name
I want to select the (categories) name.
I tried many options but nothing seems to help.
$query = "
SELECT s.name
, c.retailer_id
, b.retailer_id
FROM categories s
JOIN retailer_to_category b
ON s.category_id = b.category_id
JOIN retailers c
ON b.retailer_id = c.retailer_id
";
But that only displays the first category name in the database table, whichever retailer I select
I am building a database for an inventory management application and I have to maintain products with multiple hierarchies of category. I have the following database model :
I want to retrieve the products, description, units of measure and the categories, for which I have used the following query :
SELECT P.ID
,P.wrin
,P.description AS productDescription
,CT.pdtCat AS productCategory
,CONCAT(UN.description,' - ',UN2.description,' - ',UN3.description) AS unitDecomposition
FROM product P
-- JOIN to product_category table
JOIN (
SELECT PC3.ID as catID
,CONCAT(PC1.category,' - ',PC2.category,' - ',PC3.category) as pdtCat
FROM product_category AS PC1
LEFT JOIN product_category AS PC2 ON PC2.parentid = PC1.id
LEFT JOIN product_category AS PC3 ON PC3.parentid = PC2.id
WHERE PC1.parentid IS NULL
) CT ON CT.catID = P.categoryId
JOIN unit UN ON P.primaryUOM = UN.ID
JOIN unit UN2 ON P.secondaryUOM = UN2.ID
JOIN unit UN3 ON P.tertiaryUOM = UN3.ID
The output from this query is :
My query is giving the intended results but seems to go sideways if my product doesn't have 3 levels of categories. The product doesn't appear in the results. Please help :)
In your sub-select you are joining from the "top" category to the bottom category and select the id of the bottom one. If there is no level 3 category, there are no results.
Try something like this:
SELECT P.ID
,P.wrin
,P.description AS productDescription
,CT.pdtCat AS productCategory
,CONCAT(UN.description,' - ',UN2.description,' - ',UN3.description) AS unitDecomposition
FROM product P
-- JOIN to product_category table
JOIN (
SELECT PC3.ID as catID
,CONCAT(PC1.category,' - ',COALESCE(PC2.category, ''),' - ',COALESCE(PC3.category, '')) as pdtCat
FROM product_category AS PC3
LEFT JOIN product_category AS PC2 ON PC3.parentid = PC2.id
LEFT JOIN product_category AS PC1 ON PC2.parentid= PC1.id
) CT ON CT.catID = P.categoryId
JOIN unit UN ON P.primaryUOM = UN.ID
JOIN unit UN2 ON P.secondaryUOM = UN2.ID
JOIN unit UN3 ON P.tertiaryUOM = UN3.ID
EDIT:
My query will produce some ' - - category' results for the product category.
Due to the fact that my strength is in MSSQL, that's an exercise for you to make it look more pretty ;)
I have 3 tables as follows :
Table 1: Product
id_product [Primary Key],added_time.
Table 2: Category
id_category [Primary Key],Category_name.
Table 3: product_category
id_category,id_product [Both Foreign Keys]
I want to pull Data as
Category_name,No Of Products in this Category,Last time when product was added to Category(Latest product added_time).
You could use this SQL:
SELECT Category.Category_name,
Count(DISTINCT Product.id_product) AS num_products,
Max(Product.added_time) last_added_time
FROM Category
LEFT JOIN product_category
ON product_category.id_category = Category.id_category
LEFT JOIN Product
ON Product.id_product = product_category.id_product
GROUP BY Category.Category_name;
Note that by using LEFT JOIN you will be certain to list all categories even those for which no products exist. If you don't want those, replace both LEFT keywords with INNER.
Note also that in standard SQL you need to GROUP BY any columns you mention in the SELECT list, unless they are aggregated, like with MAX or COUNT.
SELECT C.`Category_name`,
(SUM(IF(P.`id_product`IS NULL,0,1))) AS No_of_Products,
MAX(P.`added_time`) AS Latest_time
FROM
Category C
LEFT JOIN
product_category P_C ON C.`id_category` = P_C.`id_category`
LEFT JOIN
Product P ON P.`id_product` = P_C.`id_product`
GROUP BY C.`id_category`
Hope this helps.
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)
I have 2 tables
1_products
id, code, make, model, fk_group_id
1_stock
id, stock, repair
I want to be able to return all of the rows in both tables based on the match in the first. Say WHERE fk_group_id = 11
Here is one:
SELECT *
FROM products AS p
INNER JOIN stock AS s ON p.id = s.id
WHERE fk_group_id = 11