This is my query:
UPDATE
`product_pricing`
SET `formula_id`= '2'
WHERE 1
SELECT
product_pricing.id,
product_pricing.formula_id,
product_pricing.vat_calculated,
products_ids.id,
products_ids.link_id,
product_attributes.fty_id,
product_attributes.size_id,
product_sizes.size_id,
product_sizes.wheel
FROM product_pricing
LEFT JOIN products_ids ON product_pricing.id = products_ids.id
LEFT JOIN product_attributes ON products_ids.link_id = product_attributes.fty_id
LEFT JOIN product_sizes ON product_attributes.size_id = product_sizes.size_id
WHERE
product_sizes.wheel = '13'
How would I incorporate the SELECT query with the update query?
I need to only update products that have product_sizes.wheel = '13' but to get this information, I need a few left joins.
You can use this update with LEFT JOIN and Where
update product_pricing
LEFT JOIN products_ids ON product_pricing.id = products_ids.id
LEFT JOIN product_attributes ON products_ids.link_id = product_attributes.fty_id
LEFT JOIN product_sizes ON product_attributes.size_id = product_sizes.size_id
SET product_pricing.formula_id= '2'
WHERE
product_sizes.wheel = '13'
I think an INNER JOIN is needed here:
update product_pricing
INNER JOIN products_ids ON product_pricing.id = products_ids.id
INNER JOIN product_attributes ON products_ids.link_id = product_attributes.fty_id
INNER JOIN product_sizes ON product_attributes.size_id = product_sizes.size_id
SET product_pricing.formula_id= '2'
WHERE product_sizes.wheel = '13'
try this
update product_pricing p
LEFT JOIN products_ids pi ON p.id = pi.id
LEFT JOIN product_attributes pa ON pi.link_id = pa.fty_id
LEFT JOIN product_sizes ps ON pa.size_id = ps.size_id
SET p.formula_id= '2'
WHERE
ps.wheel = '13'
Related
SELECT *
FROM `tbl_schedule_task` AS `E`
JOIN `tbl_schedule` AS `S` ON `S`.`schedule_id`=`E`.`schedule_id`
JOIN `tbl_schedule_frequency` AS `F` ON `F`.`frequency_id`=`S`.`frequency_id`
JOIN `tbl_equipments` AS `M` ON `M`.`equipment_id`=`E`.`equipment_id`
LEFT JOIN `tbl_schedule_checklist` AS `L` ON `L`.`check_list_id`=`E`.`check_list_id`
LEFT JOIN `tbl_tech_groups` AS `G` ON `G`.`group_id` = `S`.`shedule_assign_id`
LEFT JOIN `tbl_tech_technicians` AS `T` ON `T`.`techgroup_id`=`G`.`group_id`
LEFT JOIN `tbl_schedule_category` AS `SC` ON `SC`.`category_id`=`S`.`category_id`
JOIN `tbl_site_users` AS `U` ON `U`.`user_id`=`E`.`created_by`
WHERE `E`.`site_id` = '1'
AND (`E`.`approve_flg` =0
AND `E`.`check_out` = 1)
AND `E`.`approve_by` = '2'
ORDER BY `E`.`task_id` DESC
May be inner joins is returning the duplicate data. Check the inner joins before left joins of above query.
I am trying to do a left outer join to a subquery, is that possible?
Can I do something like this?:
##this is this weeks targets
select * from targets t
inner join streams s on s.id = t.stream_id
where t.week_no =WEEKOFYEAR(NOW())
left outer join
(
###############This is records selected so far this week
select p.brand_id, p.part_product_family, sum(r.best) from records r
inner join products p on p.id = r.product_id
left outer join streams s on s.body = p.brand_id and s.stream = p.part_product_family
where WEEKOFYEAR(r.date_selected) =WEEKOFYEAR(NOW())
group by p.brand_id, p.part_product_family;
) sq_2
on s.stream = sq_2.part_product_family
This is working:
##this is this weeks targets
select * from targets t
inner join streams s on s.id = t.stream_id
left outer join
(
###############This is records selected so far this week
select p.brand_id, p.part_product_family, sum(r.best) from records r
inner join products p on p.id = r.product_id
left outer join streams s on s.body = p.brand_id and s.stream = p.part_product_family
where WEEKOFYEAR(r.date_selected) =WEEKOFYEAR(NOW()) and YEAR(r.date_selected) = YEAR(now())
group by p.brand_id, p.part_product_family
) sq_2
on s.body = sq_2.brand_id and s.stream = sq_2.part_product_family
I have this query and I am getting error #1066 - Not unique table/alias: 'components'. What seems to be the issue?
SELECT COUNT(*) FROM `products`, `components`, `tradeNames`
INNER JOIN `componentsMap` ON componentsMap.product_id = product.id
INNER JOIN `components` ON componentsMap.component_id = components.id
INNER JOIN `tradeNamesMap` ON .tradeNamesMap.product_id = products.id
INNER JOIN `tradeNames` ON tradeNamesMap.tradeName_id = tradeNames.id
WHERE (((((LOWER(inci) LIKE '%abies%')
OR (trade_name.LOWER(name) LIKE '%abies%'))
OR (components.LOWER(no_cas)='abies'))
OR (components.LOWER(no_einecs)='abies'))
OR (components.LOWER(name)='abies'))
AND (`published`=1)
ORDER BY `trade_name`.`name` DESC
You don't need to list the tables before the INNER JOINs. In fact, simply don't ever use commas in the FROM clause. So:
SELECT COUNT(*)
FROM `products`
INNER JOIN `componentsMap` ON componentsMap.product_id = product.id
INNER JOIN `components` ON componentsMap.component_id = components.id
INNER JOIN `tradeNamesMap` ON tradeNamesMap.product_id = products.id
INNER JOIN `tradeNames` ON tradeNamesMap.tradeName_id = tradeNames.id
WHERE (((((LOWER(inci) LIKE '%abies%')
OR (trade_name.LOWER(name) LIKE '%abies%'))
OR (components.LOWER(no_cas)='abies'))
OR (components.LOWER(no_einecs)='abies'))
OR (components.LOWER(name)='abies'))
AND (`published`=1)
ORDER BY `trade_name`.`name` DESC;
The above query only returns one row because of the COUNT(). The order by suggests that you actually want this information for each trade_name.name. If so, you need a GROUP BY:
SELECT tn.name, COUNT(*)
FROM `products` p INNER JOIN
`componentsMap cm
ON cm.product_id = p.id INNER JOIN
`components` c
ON cm.component_id = c.id INNER JOIN
`tradeNamesMap` tnm
ON tnm.product_id = p.id INNER JOIN
`tradeNames` tn
ON tnm.tradeName_id = tn.id
WHERE ((LOWER(inci) LIKE '%abies%') OR
(tn.LOWER(name) LIKE '%abies%') OR
(c.LOWER(no_cas)='abies') OR
(c.LOWER(no_einecs)='abies') OR
(c.LOWER(name)='abies')
) AND
(`published` = 1)
GROUP BY tn.name
ORDER BY tn.`name` DESC
INNER JOIN `[components]` ON componentsMap.component_id = components.id
AND
SELECT COUNT(*) FROM `products`, [`components`], `tradeNames`
Two components are there.
Just guessing, and untested, but I suspect that something like this would do what you're after...
SELECT n.name
, COUNT(*)
FROM products p
JOIN componentsMap pc
ON pc.product_id = p.id
JOIN components c
ON c.id = pc.component_id
JOIN tradeNamesMap pn
ON pn.product_id = p.id
JOIN tradeNames n
ON n.id = pn.tradeName_id
WHERE
( inci LIKE '%abies%'
OR n.name LIKE '%abies%'
OR 'abies' IN (c.no_cas,c.no_einecs,c.name)
)
AND published = 1
GROUP
BY n.name
ORDER
BY n.name DESC
I have managed to write the following SQL that gives me all records that have the same value in _price, _regular_price & _sale_price.
I need to update _sale_price on these records only to ' ' (nothing / empty)
Here is my working Select:
SELECT p.id, p.post_title, p1.meta_key, p1.meta_value, p2.meta_key, p2.meta_value, p3.meta_key, p3.meta_value
FROM sm_posts p
INNER JOIN sm_postmeta p1 ON p.id = p1.post_id
AND p1.meta_key = '_price'
INNER JOIN sm_postmeta p2 ON p.id = p2.post_id
AND p2.meta_key = '_regular_price'
INNER JOIN sm_postmeta p3 ON p.id = p3.post_id
AND p3.meta_key = '_sale_price'
WHERE p1.meta_value = p2.meta_value AND p1.meta_value = p3.meta_value
I need to create an Update from this but cant figure it out.
So far I have this but its not working:
UPDATE sm_postmeta2,
(
SELECT p.id, p.post_title, p1.meta_key, p1.meta_value, p2.meta_key, p2.meta_value, p3.meta_key, p3.meta_value
FROM sm_posts p
INNER JOIN sm_postmeta2 p1 ON p.id = p1.post_id
AND p1.meta_key = '_price'
INNER JOIN sm_postmeta2 p2 ON p.id = p2.post_id
AND p2.meta_key = '_regular_price'
INNER JOIN sm_postmeta2 p3 ON p.id = p3.post_id
AND p3.meta_key = '_sale_price'
WHERE p1.meta_value = p2.meta_value AND p1.meta_value = p3.meta_value
GROUP BY p1.ID
)
SET sm_postmeta2.meta_value = ''
WHERE sm_postmeta2.meta_value.id = p1.id
Please can someone help, i am new to SQL and it took me forever just to get the joins working. lol
Thank you for taking the time to help me.
Your error means that you must alias the subquery. However, you don't want to put the subquery in the UPDATE clause. If you do, MySQL either won't allow it, or it will update the temporary table resulting from the subquery, i.e. the changes wouldn't persist. Instead, you can INNER JOIN on the subquery to get the rows you want to update.
Since you're just using the subquery to determine the rows to update, you don't need to SELECT all the same values as before. Instead, you can just SELECT the primary key for the records you want to update.
Here's an example:
UPDATE sm_postmeta2 INNER JOIN
(
SELECT DISTINCT p3.meta_id
FROM sm_posts p
INNER JOIN sm_postmeta2 p1 ON p.id = p1.post_id
AND p1.meta_key = '_price'
INNER JOIN sm_postmeta2 p2 ON p.id = p2.post_id
AND p2.meta_key = '_regular_price'
INNER JOIN sm_postmeta2 p3 ON p.id = p3.post_id
AND p3.meta_key = '_sale_price'
WHERE p1.meta_value = p2.meta_value AND p1.meta_value = p3.meta_value
) p_tmp ON sm_postmeta2.meta_id = p_tmp.meta_id
SET sm_postmeta2.meta_value = ''
SELECT order.date,
vehicle.year,
vehicle.make,
vehicle.model,
SUM(part.price * order_details.quantity) AS price,
order.paid
FROM vehicle
INNER JOIN user
ON user.id = vehicle.user_id
INNER JOIN order
ON order.user_id = user.id
INNER JOIN order_details
ON order_details.order_id = order.id
INNER JOIN part
ON part.id = order_details.part_id
ORDER is a reserved keyword in most SQL implementations; you probably need to delimit the tablename using a backtick.
SELECT `order`.date,
vehicle.year,
vehicle.make,
vehicle.model,
SUM(part.price * order_details.quantity) AS price,
`order`.paid
FROM vehicle
INNER JOIN user
ON user.id = vehicle.user_id
INNER JOIN `order`
ON `order`.user_id = user.id
INNER JOIN order_details
ON order_details.order_id = `order`.id
INNER JOIN part
ON part.id = order_details.part_id