MySQL LEFT JOIN where multiple values - mysql

I have query that joins Products table on Product_Attributes table. I' would like to get products which meat exact condition ex.
Products which have attribute id 9 AND 16 selected, but when I run this query I'll get only product which meets first ID
WHERE ("AttributeValueID" = 9 AND "AttributeValueID" = 16)
I need that products to have both attributes selected AttributeValueID = 9 AND AttributeValueID = 9 not only this or that attribute ID. Using OR / IN is'not solution for me.
Update:
below is a part of query with join / where:
LEFT JOIN "Product_Attributes" ON "Product_Attributes"."ProductID" = "SiteTree_Live"."ID" WHERE ("PriceTax" >= '100' AND "PriceTax" <= '200') AND ("AttributeValueID" IN ('9','8','7')) AND ("AttributeValueID" IN ('5','4'))
thank you in advance for your help

This is an example of a "set-within-sets" query. I like to solve these using group by and having, because that is a very flexible approach:
select pa.ProductId
from ProductAttributes pa
group by pa.ProductId
having sum(AttributeValueId = 9) > 0 and
sum(AttributeValueId = 16) > 0;

Gordon's answer showed the most flexible way to get the list of ProductId values that meet the specified criteria.
You could use a query like Gordon's (that gets the set of ProductId) as an inline view, and do a JOIN operation against the products table, like this:
SELECT p.ProductID
, p.ProductName
, ...
FROM products p
JOIN (
SELECT a.ProductID
FROM ProductAttributes a
GROUP BY a.ProductId
HAVING SUM(a.AttributeValueID=9) > 0
AND SUM(a.AttributeValueID=16) > 0
) s
ON s.ProductID = p.ProductID
UPDATE
based on the edit to the question adding a snippet of SQL text:
JOIN ( SELECT a.ProductID
FROM Product_Attributes a
HAVING SUM(a.AttributeValueID IN ('9','8','7')) > 0
AND SUM(a.AttributeValueID IN ('5','4')) > 0
GROUP BY a.ProductID
) s
ON s.ProductID = SiteTree_Live.ID
WHERE SiteTree_Live.PriceTax >= '100'
AND SiteTree_Live.PriceTax <= '200'

Related

SQL TEST: OUTER JOIN a prod tbl & compOrder tbls. Display id & prodname frm product tbl, display id & totAmt fr compOrder tbl where both tbl id<10

I have a prod table and compOrder table. I need to Perform an OUTER JOIN on the product and completedOrder tables.
Display the id and productname from the product table and the id and totalAmount from the completedOrder table where the id is less than 10 in both tables.
This is what I have thus far. I'm New, learning and need guidance. Thanks in advance - amanda
SELECT product.id,
product.productName,
completedOrder.id,
completedOrder.totalAmount
FROM completedOrder
FULL JOIN product
ON product.id < '10' = completedOrder.id < '10'
ORDER BY productName;
You use 2 tables prod and compOrder. What is the relationship between both table?.
Let us assume product.id is in compOrder table as product_id[foreign key].
You have to change your query like this,
SELECT p.id, p.productName, c.id,c.totalAmount
FROM completedOrder as c
JOIN product as p
ON p.id=c.product_id
WHERE p.id < '10' AND c.id < '10'
ORDER BY p.productName;

WHERE AND OR issue to get the checked items

I am trying to create a dynamic query that joins my tables and filters the filters selected. In my example, the products_bind_filters are the filter items that belong to each product.
I'm trying to find the products that have the following criteria as an example in one result:
Products that are Red, Green OR Blue
that are also available in Small or Medium
My query is:
SELECT *,
p.name as productName,
c.name as categoryName,
p.price as productPrice,
p.hookName as productHook,
c.hookName as categoryHook,
p.imageMain as productThumb
FROM products p
JOIN products_bind_category pbc ON pbc.productsId = p.productsId
JOIN category c ON pbc.categoryId = c.categoryId
JOIN products_bind_value pbv ON pbv.productsId = p.productsId
WHERE p.productsId != '0'
AND pbc.categoryId = '10'
AND (pbv.valueId = '54' ) AND (pbv.valueId = '167' OR pbv.valueId = '186' OR pbv.valueId = '175' )
GROUP BY p.productsId
ORDER BY p.Price ASC
However, it's not returning the correct results. There are products that match the criteria but it's not showing them.
Any ideas?
I did try use an IN query but it also didn't work.
You are working with a key/value table, which is always kind of hard.
This condition:
AND (pbv.valueId = '54')
AND (pbv.valueId = '167' OR pbv.valueId = '186' OR pbv.valueId = '175')
is never met, because no row in the table can have a value of 54 and not 54 at the same time.
One way to deal with key/values is aggregation:
select *
from products p
join products_bind_category pbc on pbc.productsid = p.productsid
join category c on pbc.categoryid = c.categoryid
where p.productsid in
(
select productsid
from products_bind_value
group by productsid
having sum(valueid = 54) > 0
and sum(valueid in (167, 196, 175)) > 0
)
order by p.productsid;
The SUM(<expression>) > 0) uses the fact that in MySQL true = 1, false = 0 by the way.

Use both operation (or) with (and) on the same field in SQL ( not different fields)

I have three tables, many to many relationship
the tables will be like the following if I insert data
I want to get the product name where (propertyValue='ios' or propertyValue = 'android') and (propertyValue='black')
In another way, we can say
I want to get the product name where (propertyid='2' or propertyid = '4') and (propertyid='3')
I tried second way and there is a SQL:
Select DISTINCT(p.productname),p.productid
from Products p
left join Product_propertyvalue ppv on p.productid=ppv.productid
where ( ppv.propertyvalueid=2 or ppv.propertyvalueid=4) and ( ppv.propertyvalueid=3 )
You can use having:
select p.productname, p.productid
from Products p left join
Product_propertyvalue ppv
on p.productid = ppv.productid
group by p.productname, p.productid
having sum( ppv.propertyvalueid in (2, 4) ) > 0 and
sum( ppv.propertyvalueid = 3 ) > 0;
The sum() conditions check that each property is there for a given product.

MySQL update from nested SELECT query

I have 2 tables which are getting different values from different sources.
One table called stock has 2 fields, code and stock, while other have code, stock (and bunch more not relevant to what I want to do)
Basically my current query for checking if there are enough quantities is following
SELECT *
FROM products
WHERE products.code
NOT IN (SELECT stock.code from stock)
AND products.stock > 0
AND products.product_active = 1
Which works, but I would like to extend it to UPDATE stock field of the table products to value 0 for the results of the above query.
Thanks
Try this:
UPDATE products p
LEFT JOIN stock s ON p.code = s.code
SET p.stock = 0
WHERE p.stock > 0 AND p.product_active = 1 AND s.code IS NULL
So why don't you do it? Its very similar to what you just did :
UPDATE products p
SET p.stock = 0
WHERE p.code NOT IN(SELECT s.code FROM stock s)
AND p.stock > 0
AND p.product_Active = 1
I don't like to use IN() statement, since it doesn't handle NULL values very well, you can also use NOT EXISTS() instead of it:
UPDATE products p
SET p.stock = 0
WHERE NOT EXISTS(SELECT 1 FROM stock s
WHERE p.code = s.code)
AND p.stock > 0
AND p.product_Active = 1

MYSQL - JOIN table filter not working

I have four table each table connected with some id kindly see the below query how I'm getting
SELECT
tax_rates.name,
IF( products.tax_method = 0, 'Inclusive', 'Exclusive') AS type,
IF((SUM(purchases.grand_total) > 0),(SUM(purchases.grand_total)),(SUM(purchase_items .subtotal))) AS total_amount,
IF((SUM(purchases.order_tax) > 0),(SUM(purchases.order_tax)),SUM(purchase_items .item_tax)) AS tax_amount
FROM tax_rates
LEFT JOIN purchases ON purchases.order_tax_id = tax_rates.id
LEFT JOIN purchase_items ON purchase_items.tax_rate_id = tax_rates .id
LEFT JOIN products ON products.id = purchase_items.product_id
WHERE purchases.warehouse_id = 1
GROUP BY tax_rates.id, products.tax_method
ORDER BY tax_rates.name desc LIMIT 0, 10
The above query didn't return any result but if I remove WHERE purchases.warehouse_id = 1 then it display the result. I don't know where I'm doing the mistake. Kindly correct me.
Sorry to tell about this I'm try to get purchase order tax and purchase Item tax
in particular store and date
Output
name type total_amount tax_amount
VAT #20% Inclusive 11005.2000 1834.2000
VAT #10% Inclusive 165.0000 15.0000
No Tax Exclusive 204771.4000 0.0000
GST #6% Exclusive 7155.0000 405.0000
GST #6% Inclusive 7155.0000 405.0000
Thank you
Data type? purchases.warehouse_id = '1'?
You should not add the condition in where clause, when you are using the field of the left join table. so remove the where clause and use like below
LEFT JOIN purchases ON purchases.order_tax_id = tax_rates.id and purchases.warehouse_id = 1
If you try to add the left join table field in where clause it will become INNER JOIN. hence it may not display any rows according to your data.