I know this is simple but I'm have a mental block.
I need to update all products that belongs to a category. The product is assigned to a category in an association table.
so it's something like
UPDATE product P1
SET myflag = 1
WHERE P1.productid IN (
SELECT CA.productid
FROM category_associations CA
WHERE CA.categoryid = '500'
)
is there a better way?
A JOIN will typically have the optimal execution plan, but IN can be inefficient:
UPDATE
product P1 JOIN
category_associations ca
ON ca.productId = P1.productId
AND ca.categoryId = '500'
SET P1.myflag = 1
another way to write it is:
UPDATE product
SET myflag = 1
FROM product P1 JOIN
category_associations ca
ON ca.productId = P1.productId
WHERE ca.categoryId = '500'
Related
I building a custom SQL query for my module to retrieve all combinations of a product with id_product and multiple attributes ids, but currently, I only managed to select it with one attribute and no more, I'm really missing something but didn't find it yet.
To get in context here's my query to find all combinations (color & size) of a product and its result:
SELECT
p.id_product,
pq.quantity,
pa.price AS price_diff,
p.price,
pai.id_image,
pl.name,
GROUP_CONCAT(agl.id_attribute_group, ':', pal.id_attribute ORDER BY agl.id_attribute_group SEPARATOR ", ") as combination_ids,
GROUP_CONCAT(pal.name ORDER BY agl.id_attribute_group SEPARATOR ", ") as combination
FROM ps_product p
LEFT JOIN ps_product_attribute pa ON (p.id_product = pa.id_product)
LEFT JOIN ps_stock_available pq ON (p.id_product = pq.id_product AND pa.id_product_attribute = pq.id_product_attribute)
LEFT JOIN ps_product_lang pl ON (p.id_product = pl.id_product)
LEFT JOIN ps_product_attribute_combination pac ON (pa.id_product_attribute = pac.id_product_attribute)
LEFT JOIN ps_attribute_lang pal ON (pac.id_attribute = pal.id_attribute)
LEFT JOIN ps_attribute a ON (pal.id_attribute = a.id_attribute)
LEFT JOIN ps_attribute_group_lang agl ON (a.id_attribute_group = agl.id_attribute_group)
LEFT JOIN ps_product_attribute_image pai on(pa.id_product_attribute = pai.id_product_attribute)
WHERE pl.id_lang = 1
AND pal.id_lang = 1
AND agl.id_lang = 1
AND p.id_product = 3196 -- My product
GROUP BY pac.id_product_attribute
The result
Query with a single attribute (size S for this example):
......................
......................
AND p.id_product = 3196 -- My product
AND agl.id_attribute_group = 9 -- size
AND pal.id_attribute = 761 -- 'S' size for my case
GROUP BY pac.id_product_attribute
But no success with specifying both size AND color, any idea?
I think you want a HAVING clause. To filter on two attributes, the logic would be:
SELECT ...
FROM ...
WHERE ...
GROUP BY pac.id_product_attribute
HAVING
MAX(agl.id_attribute_group = 9 AND pal.id_attribute = 761) = 1
AND MAX(agl.id_attribute_group = 2 AND pal.id_attribute = 727) = 1
I should warn that your code is not a valid aggregation query. You need more column in the GROUP BY clause to fix that flaw. It is hard to tell for sure without seeing your data, but, with a few assumptions on the primary key of each table:
GROUP BY
p.id_product,
pa.id_product_attribute,
pac.id_product_attribute,
pai.id_image,
pq.id -- if that exists?
I have the SQL to display ALL the activities and relative Admin permissions (if any) for that activity.
Current SQL Code:
SELECT `activities`.*, `admins`.`admin_role_id`
FROM (`activities`)
LEFT JOIN `admins` ON `admins`.`activity_id`=`activities`.`id` AND admins.member_id=27500
WHERE `activities`.`active` = 1
Returning:
id | name | description | active | admin_role_id (or null)
I then need to detect whether they are an active member within that Activity.
I have the following SQL code:
SELECT DISTINCT `products`.`activity_ID` as joinedID
FROM (`transactions_items`)
JOIN `transactions` ON `transactions`.`id` = `transactions_items`.`id`
JOIN `products` ON `products`.`id` = `transactions_items`.`product_id`
JOIN `activities` ON `activities`.`id` = `products`.`activity_ID`
WHERE `transactions`.`member_id` = 27500
AND `activities`.`active` = 1
Is there any way to merge this into one SQL query. I can't figure out how to use the correct JOIN queries, because of the complexity of the JOINs.
Help please, thanks! :)
Try like this
SELECT `activities`.*, `admins`.`admin_role_id`
FROM (`activities`)
LEFT JOIN `admins` ON `admins`.`activity_id`=`activities`.`id` AND admins.member_id=27500
JOIN (`transactions_items`
JOIN `transactions` ON `transactions`.`id` = `transactions_items`.`id`
JOIN `products` ON `products`.`id` = `transactions_items`.`product_id`)
ON `activities`.`id`=`products`.`activity_ID`
WHERE `transactions`.`member_id` = 27500
AND `activities`.`active` = 1
Seems to me that a query like this would be marginally more comprehensible and (I think) adhere more closely to the spec...
SELECT c.*
, d.admin_role_id
FROM activities c
LEFT
JOIN admins d
ON d.activity_id = c.id
AND d.member_id = 27500
LEFT
JOIN products p
ON p.activity_ID = c.id
LEFT
JOIN transactions_items ti
ON ti.product_id = p.id
LEFT
JOIN transactions t
ON t.id = ti.id
AND t.member_id = 27500
WHERE c.active = 1
I've got the following Mysql structure:
jss_products
productID
price1
jss_extrafields_values
exvalID
productID
extraFieldID
jss_extrafields_prices
exvalID
price1
Each product has a few extrafields. I'm interested in extraFieldID = 1
I wish to update all of the price1 in jss_extrafields_prices using the value of jss_products.price1. I have the following query but it only updates the first entry per product in jss_extrafields_price, not all entries.
I'm trying to normalize the prices in jss_extrafields_prices so that for a product which has a price of 20.00, each relevant entry in jss_extrafields_prices becomes CURRENTPRICE - 20.
Does that make sense? Here's what I have so far
UPDATE jss_extrafields_prices AS JEP
INNER JOIN (
SELECT P.productID, P.price1 AS P1, EP.price1, EP.exvalID FROM jss_products AS P
INNER JOIN jss_extrafields_values AS EV
ON P.productID = EV.productID
INNER JOIN jss_extrafields_prices AS EP
ON EV.exvalID = EP.exvalID
WHERE EV.extraFieldID = 1
GROUP BY P.productID
ORDER BY P.productID DESC, EP.price1 DESC
) AS X
ON JEP.exvalID = X.exvalID
SET JEP.price1 = JEP.price1 - X.P1
I would expect the inner query to return something like:
productID = 1090
P1 = 20.8333333
price1 = 20.8333333
exvalID = 3236
Knowing that productID of 1090 has 3 pricing options and its base price is 20.83333 I would then want to update every matching product in jss_extrafields_prices to be the current price minus the base price.
Does that help?
Stripping away some of the information that was previously classes as being too localized, this simplified to an UPDATE with an INNER JOIN:
UPDATE jss_extrafields_prices
INNER JOIN (
SELECT P.productID, P.price1 AS baseprice, JEP.price1 AS optionprice, JEP.exvalID, (JEP.price1 - P.price1) AS adjustedprice FROM jss_products AS P
INNER JOIN jss_extrafields_values AS JEV
ON P.productID = JEV.productID
INNER JOIN jss_extrafields_prices AS JEP
ON JEV.exvalID = JEP.exvalID
WHERE JEV.extraFieldID = 1
) AS X
ON jss_extrafields_prices.exvalID = X.exvalID
SET jss_extrafields_prices.price1 = X.adjustedprice
Basically I have product and several models for those products. Each model has a price.
This is what I intended to do:
Mark a product as featured, then have it's title, description, number 1 image's thumbnail and the price for the cheapest model
This is my current query:
SELECT
product.title,
product.url_name,
product.description,
price.price,
image.thumbnail
FROM
mps_contents AS product
LEFT OUTER JOIN
mps_contents AS image
ON
image.page_id = product.content_id AND
image.display_order = '1' AND
image.resource_type = 'image'
LEFT OUTER JOIN
mps_contents AS model
ON
product.content_id = model.page_id
INNER JOIN
mps_product_info AS price
ON
model.content_id = price.content_id
WHERE
product.active = '1' AND
product.resource_type = 'product' AND
product.featured = '1'
ORDER BY RAND( )
LIMIT 3
You may see that my query cannot do the price sorting, I hope somebody could help me with that. An additional problem that I encounter is if I have multiple models for a product. I end up getting a set that has prices for 2 models from a single product when the intent is to have the 1 price for each product.
I am aware of the issue with ORDER BY RAND() but I will ignore it since I don't think this site will have more that 50 products.
I think something like this should work....
SELECT
product.title,
product.url_name,
product.description,
A.price,
image.thumbnail
FROM
mps_contents AS product
LEFT OUTER JOIN
mps_contents AS image
ON
image.page_id = product.content_id AND
image.display_order = '1' AND
image.resource_type = 'image'
LEFT OUTER JOIN (
SELECT price.price
FROM mps_contents AS model
JOIN mps_product_info price ON (model.content_id = price.content_id)
WHERE model.page_id = product.content_id
ORDER BY price.price
LIMIT 1
) AS A
WHERE
product.active = '1' AND
product.resource_type = 'product' AND
product.featured = '1'
ORDER BY RAND( )
LIMIT 3
basically i have this structure:
Deal has and belongs to many Channels
Deal has many DealSales
Deal belongs to Channel
When i want to find the amount sold by a deal, i use this query:
SELECT targets.id,SUM(deal_sales.amount_sold) AS amount_sold
FROM deal_sales
INNER JOIN deals ON deals.id = deal_sales.deal_id
INNER JOIN targets ON deals.target_id = targets.id
WHERE targets.approved = 1 AND targets.active = 1
GROUP BY targets.id
its working just fine, the problem is when i need to filter by channel, find the amount sold by a deal in a channel:
SELECT targets.id,SUM(deal_sales.amount_sold) AS amount_sold
FROM deal_sales
INNER JOIN deals ON deals.id = deal_sales.deal_id
INNER JOIN targets ON deals.target_id = targets.id
**INNER JOIN channels_deals ON channels_deals.deal_id = deals.id**
WHERE targets.approved = 1 AND targets.active = 1
GROUP BY targets.id
When i add the join to channels table, the amount_sold is multiplied by each channel a deal has relation with. How can i avoid this?
Use IN or Exists
for example
SELECT targets.id,SUM(deal_sales.amount_sold) AS amount_sold
FROM deal_sales
INNER JOIN deals ON deals.id = deal_sales.deal_id
INNER JOIN targets ON deals.target_id = targets.id
WHERE targets.approved = 1 AND targets.active = 1
and
deals.id IN (SELECT deal_id from channels_deals where something = 1)
GROUP BY targets.id
Try this:
SELECT channels_deals.deal_id,SUM(deal_sales.amount_sold) AS amount_sold
FROM deal_sales
INNER JOIN deals ON deals.id = deal_sales.deal_id
INNER JOIN targets ON deals.target_id = targets.id
INNER JOIN channels_deals ON channels_deals.deal_id = deals.id
WHERE targets.approved = 1 AND targets.active = 1
GROUP BY channels_deals.deal_id