I have tables like this:
tbl_product
===========
product_id (PK)
tbl_product_attribute
=====================
pro_attr_id (PK)
pro_attr_pro_id (FK to tbl_product)
pro_attr_attr_opt_id(FK to tbl_attribute_option)
Now, I would like query Products which have 2 attributes in the tbl_product_attribute.
Example like :
SELECT "p".*
FROM "tbl_omx_product" "p"
LEFT JOIN "tbl_omx_product_attribute" "proAttr" ON "proAttr".pro_attr_pro_id = p.product_id
WHERE
(pro_attr_attr_opt_id LIKE '%1759%' ) AND
(pro_attr_attr_opt_id LIKE '%1776%' )
GROUP BY "p"."product_id";
So I'd like to get Products that has exactly 2 values at tbl_omx_product_attribute, which is 1759 & 1776.
But query like above won't show any result unless I use relation OR instead of AND.
The question what is the query to retrieve Products that have 2 values at the tbl_product_attribute ? thank you
Maybe I misunderstood you, but if you want the product to have both attribute options you can query:
SELECT "p".*
FROM "tbl_omx_product" "p"
JOIN "tbl_omx_product_attribute" "proAttr1"
ON "proAttr1".pro_attr_pro_id = p.product_id
AND "proAttr1".pro_attr_attr_opt_id LIKE '%1759%'
JOIN "tbl_omx_product_attribute" "proAttr2"
ON "proAttr2".pro_attr_pro_id = p.product_id
AND "proAttr2".pro_attr_attr_opt_id LIKE '%1776%'
To get products which have two attributes:
SELECT p.*
FROM tbl_omx_product p
LEFT JOIN tbl_omx_product_attribute proAttr ON proAttr.pro_attr_pro_id = p.product_id
GROUP BY p.product_id
having count(*)=2;
You can filter data as per your required output by just adding where clause.
You can try this:
SELECT p.*
FROM tbl_omx_product AS p
LEFT JOIN tbl_omx_product_attribute AS proAttr ON p.product_id = proAttr.pro_attr_pro_id
WHERE
(pro_attr_attr_opt_id LIKE '%1759%' ) AND
(pro_attr_attr_opt_id LIKE '%1776%' )
GROUP BY p.product_id;
Related
I'm a bit of a noob and I'm sure this is fairly simple but I've tried different things and I can't get my head around it.
I have a product table that contains a category id and I want to set the quantity to 0 which is stored in another table.
This is the last thing I tried:
function wipeprod(){
UPDATE stock_available
INNER JOIN
product stock_available
ON
(
product.id_category_default != 3
OR
product.id_category_default != 10
OR
product.id_category_default != 943
)
SET
stock_available.quantity = 0;
}
Thanks in advance.
I suspect that you want to set column quantity in table stock_available to 0 for products that are not in categories 3, 10, and 943.
If so, you can use the update ... join syntax as follows:
update stock_available s
inner join product p on p.product_id = s.product_id
set s.quantity = 0
where p.id_category_default not in (3, 10, 943)
The main problems with your original query are:
the join is missing a predicate on product_id
you wanted AND condition rather than OR; NOT IN comes handy to shorten this.
You can use such an update statement by use of INNER JOIN :
UPDATE stock_available s
JOIN
(
SELECT id
FROM product
WHERE id_category_default NOT IN (3,10,943)
) AS p ON p.id = s.product_id
SET
s.quantity = 0;
where no need a proper query after the keyword JOIN
I need join values from my products table only when record in changes table exist, on other ways get return only from products table, I trying use LEFT JOIN for it but doing something wrong. Also tables changes and products have same structure
SELECT
product.*,
parent.name,
parent.ignored,
child.name,
child.ignored,
changes.*
FROM
products AS product
inner join parent_cat AS parent on product.parent_id = parent.ID
inner join child_cat AS child on product.category_id = child.ID
left join changes AS changes on changes.product_id = product.SKURcrd
WHERE
product.slug = 'some slug'
Returned data looks like:
{#389 ▼
+"parent_id": 3
+"category_id": 142
+"product_id": null
+"slug": "some slug"
+"SKURcrd": "301832"
+"product_name": null
+"product_name_revert": "1911 AIR"
}
Tables Structure looks like (schematic):
------------------------- ----------------------------
SKURcrd | product_name product_id | product_name
------------------------- ----------------------------
Two ways you can do that, either use INNER JOIN to changes or a where clause like where changes. produc_id is not null... like below:
SELECT
product.*,
parent.name,
parent.ignored,
child.name,
child.ignored,
changes.*
FROM
products AS product
inner join parent_cat AS parent on product.parent_id = parent.ID
inner join child_cat AS child on product.category_id = child.ID
left join changes AS changes on changes.product_id = product.SKURcrd
WHERE
product.slug = 'some slug' and changes.product_id is not null
How can I show the products ( description , serial number, price ) purchased by the customer "Peacock cafe bar" in the picture
I show 3 tables
I have try with
select
from ...
where(
select
from ...
as ...
where
“”
)
Try something like this: You'll want to join your tables on their keys and then filter the results in the WHERE clause like so.
SELECT
p.pr_serial
, p.pr_descr
, p.pr_price
FROM Customers AS c
JOIN Buys AS b
ON c.cust_code = b.cust_code
JOIN Products AS p
ON b.pr_code = p.pr_code
WHERE c.cust_name = 'Peacock cafe bar';
As I remember you should use joint query on your tables, something like:
select * from ProductsTable as Products
join
(select Buys.ProCode from CustomerTable as Customer
join BuysTable as Buys
on Customer.code = Buys.custcode) as customerbuys
on Products.code = customerbuys.ProCode
p.slugUrl AS resultslug, p.id, m.slugUrl, m.categoryParent, pa.pageId, pa.categoryId from tcs_pagecontents as p,tcs_pageassociation as pa, tcs_menucategory_site as m where p.id=pa.pageId and pa.categoryId=m.id ;
This is returning a table with following structure( lets say this table as P)
resultslug
id
slugUrl
categoryParent
pageId
categoryId
Further I wanted to make a join with this resulted table to another table where P.categoryParent = T.id, here T is another table I wanted to fetch the following coloumn name
P.resultslug
P.id
P.slugUrl
P.categoryParent
P.pageId
P.categoryId
T.slugUrl
where P.categoryParent = T.id;
I am getting blank here. Any Idea will be appreciated. Thanks in advance.
Just join that table like the other ones.
select p.slugUrl AS resultslug, p.id, m.slugUrl, m.categoryParent, pa.pageId, pa.categoryId, T.slugURL
from tcs_pagecontents as p
join tcs_pageassociation as pa on p.id=pa.pageId
join tcs_menucategory_site as m on pa.categoryId=m.id
join another_table T on m.categoryParent = T.id;
I'm having difficulty with some SQL - and finding it hard to describe so please bear with me. I'm trying to select products that have an x number of correct features. To simplify things, the problem I'm having is with a few tables. The relevant information about my tables, is:
products(**product_id**)
features(**feature_id, product_id**, value_id)
featurenames(**feature_id**, name)
featurevalues(**value_id**, value)
I am trying to select all products that, for example, are for sex:male age:children, with age and sex being names in the featurenames table and male and children being values in the featurevalues table. This is part of another bigger query that gets all the products by category, which I haven't included as it will just complicate things. Thank you for your help in advance.
It's the table so nice, you join to it twice.
Select
P.Product_ID
From
Products P
Inner Join Features F1 On P.Product_ID = F1.Product_ID
Inner Join FeatureNames FN1 On F1.Feature_ID = FN1.Feature_ID And FN1.Name = 'sex'
Inner Join FeatureValues FV1 On F1.Value_ID = FV1.Value_ID And FV1.Value = 'male'
Inner Join Features F2 On P.Product_ID = F2.Product_ID
Inner Join FeatureNames FN2 On F2.Feature_ID = FN2.Feature_ID And FN1.Name = 'age'
Inner Join FeatureValues FV2 On F2.Value_ID = FV2.Value_ID And FV1.Value = 'children'