Is it possible to do a select and join a table, then update that table?
I'm basically looking to increase the price by 5% on all products owned by a particular supplier. The problem is the pricing details are held in a separate table to the products themselves.
I know this syntax is not correct, but it will give you the gist of what i'm trying to achieve:
update
products_quantity_pricing set price = price + (price/100*5)
where (select
products.supplier_id,
products_quantity_pricing.price
from products
join
products_quantity_pricing on products_quantity_pricing.product_id = products.id
where products.supplier_id = 7 )
You put the JOIN directly into the UPDATE query:
UPDATE products_quantity_pricing AS pqp
JOIN products AS p ON pqp.product_id = p.id
SET price = price + (price/100*5)
WHERE p.supplier_id = 7
update products_quantity_pricing set price = price + (price/100*5)
where products_quantity_pricing.price in
(select products_quantity_pricing.price
from products join
products_quantity_pricing on products_quantity_pricing.product_id = products.id
where products.supplier_id = 7 )
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 am trying to convert following SQL into rails active record query
"SELECT * FROM stocks
INNER JOIN
(
SELECT product_id, MIN(ask_price) min_price
FROM stocks
GROUP BY product_id,size
) sub ON stocks.product_id = sub.product_id AND
stocks.ask_price = sub.min_price
where stocks.product_id = 1"
This query fetches the lowest price of the stock group by product and size of product.
So far I have tried to translate it like this it not right.
sub_query = Stock.select("product_id, MIN(ask_price) min_price").group(:product_id,:size)
stocks = Stock.joins("#{sub_query} stocks.product_id = sub.product_id AND
stocks.ask_price = sub.min_price")
You should pass into joins method all your join clause if you want to do some custom joining, like this:
stocks = Stock.joins(<<-SQL
INNER JOIN
(
SELECT product_id, MIN(ask_price) min_price
FROM stocks
GROUP BY product_id, size
) sub ON stocks.product_id = sub.product_id AND stocks.ask_price = sub.min_price
SQL).where(product_id: 1)
I have the following query for a report all is working fine, but I need to add a variable in the report that totals up the number of records for each record returned based off the number of records in the "manheim_auction_listings" record. I feel like it needs to be inside a join but everywhere I would the "COUNT(*) AS num_of_runs" it seems to make the whole query only return a single line with the count the total number of records in the query rather than all the lines with a variable num_of_runs with the number of "manheim_auction_listings" records for each CAR record.
SELECT products.client_id,
clients.name AS client_name,
manheim_auction_lanes.lane_number,
manheim_auction_listings.sequence,
manheim_auction_listings.gross_sale_price,
products.asking_price, products.asking_price_condition,
manheim_auctions.auction_date,
manheim_auctions.auction_number,
product_purchases.total_spent,
product_purchases.purchase_price
FROM manheim_auction_listings
JOIN cars ON
cars.id = manheim_auction_listings.car_id
JOIN products ON
cars.product_id = products.id
JOIN product_purchases ON
current_product_purchase_id = product_purchases.id
JOIN manheim_auctions ON
manheim_auctions.id = manheim_auction_listings.manheim_auction_id
JOIN manheim_auction_lanes ON
manheim_auction_lanes.id = manheim_auction_listings.manheim_auction_lane_id
JOIN clients ON
clients.id = products.client_id
AND clients.id LIKE $P{LoggedInUserAttribute_ClientID}
WHERE
manheim_auctions.auction_number = $P{SaleNumber}
AND manheim_auctions.`year` = $P{SaleYear}
ORDER BY manheim_auction_lanes.lane_number DESC,
manheim_auction_listings.sequence DESC
Please try the following...
SELECT products.client_id,
clients.name AS client_name,
manheim_auction_lanes.lane_number,
manheim_auction_listings.sequence,
manheim_auction_listings.gross_sale_price,
num_of_runs,
products.asking_price, products.asking_price_condition,
manheim_auctions.auction_date,
manheim_auctions.auction_number,
product_purchases.total_spent,
product_purchases.purchase_price
FROM ( SELECT manheim_auction_id AS manheim_auction_id,
COUNT( manheim_auction_id ) AS num_of_runs
FROM manheim_auction_listings
GROUP BY manheim_auction_id
) AS num_of_runs_finder
JOIN manheim_auction_listings ON manheim_auction_listings.manheim_auction_id = num_of_runs.manheim_auction_id
JOIN cars ON cars.id = manheim_auction_listings.car_id
JOIN products ON cars.product_id = products.id
JOIN product_purchases ON current_product_purchase_id = product_purchases.id
JOIN manheim_auctions ON manheim_auctions.id = manheim_auction_listings.manheim_auction_id
JOIN manheim_auction_lanes ON manheim_auction_lanes.id = manheim_auction_listings.manheim_auction_lane_id
JOIN clients ON clients.id = products.client_id
AND clients.id LIKE $P{LoggedInUserAttribute_ClientID}
WHERE manheim_auctions.auction_number = $P{SaleNumber}
AND manheim_auctions.`year` = $P{SaleYear}
ORDER BY manheim_auction_lanes.lane_number DESC,
manheim_auction_listings.sequence DESC
This works by joining your other tables to one that calculates the number of listings associated with each manheim_auction_id, effectively appending a manheim_auction_id's count to each row where that manheim_auction_id occurs.
If num_of_runs is calculated on some other criteria, then please advsie me accordingly.
If you have any questions or comments, then please feel free to post a Comment accordingly.
I have a client that wants to have all products that do not have a price to show up behind all products that do have a price. In this case, I will change the product position to 999. But they have over 3,000 products and it will take me hours to do manually.
Is there a way to possibly update this through MySQL? I would have to connect two tables I know. catalog_category_product and catalog_product_entity_decimal I believe it is.
Now I know catalog_category_product has a field called product_id and catalog_product_entity_decimal has a field called entity_id which are essentially the same thing.
I figure there has to be a way to connect each of them somehow to make this happen.
EDIT: And yes, I know there is a Sort By that will do this, but they don't want to have the highest price to the lowest price showing as a default. They want to be able to position items they would like on the first fold.
1. Always do database backup before this kind of operation
2. use this to check if your result is correct:
SELECT *
FROM `catalog_category_product` AS `cpe`
INNER JOIN (
SELECT `e`.`entity_id`
FROM `catalog_product_entity` AS `e`
INNER JOIN `eav_attribute` AS `ea` ON `ea`.`attribute_code` = 'price'
INNER JOIN `catalog_product_entity_decimal` AS `at_price` ON (`at_price`.`entity_id` = `e`.`entity_id`) AND (`at_price`.`attribute_id` = `ea`.`attribute_id`)
WHERE ((at_price.value = '0') OR (((at_price.value = 'price') OR (at_price.value = '1'))))
) AS `result` ON cpe.product_id = result.entity_id;
3. and this to update position values:
UPDATE `catalog_category_product` AS `cpe`
INNER JOIN (
SELECT `e`.`entity_id`
FROM `catalog_product_entity` AS `e`
INNER JOIN `eav_attribute` AS `ea` ON `ea`.`attribute_code` = 'price'
INNER JOIN `catalog_product_entity_decimal` AS `at_price` ON (`at_price`.`entity_id` = `e`.`entity_id`) AND (`at_price`.`attribute_id` = `ea`.`attribute_id`)
WHERE ((at_price.value = '0') OR (((at_price.value = 'price') OR (at_price.value = '1'))))
) AS `result` ON cpe.product_id = result.entity_id
SET `cpe`.`position` = 999;
4. Notice this code shouldn't work with multistores.
5. Use it on your own risk:)
i'm trying to get 'business_id' and 'rating' from the table 'reviews' where 'review_id' is $approveReviewID
and then update 'rating' in 'business_details' with this retrieved rating from 'reviews' where 'business_id' is equal to that retrieved.
i have these two queries,
SELECT business_id, rating FROM reviews WHERE review_id = '$approveReviewID';
UPDATE business_details SET rating = rating + $rating WHERE business_id = '$businessID';
is there a way to join these queries into one?
Yes, you can use the update-join syntax:
UPDATE business_details bd
JOIN reviews r ON bd.business_id = r.business_id
SET bd.rating = bd.rating + r.rating
WHERE r.review_id = '$approveReviewID'
Use simple "Inner join" in from clause to join both tables
UPDATE bd
SET rating = bd.rating + rvs.rating
FROM reviews rvs
INNER JOIN business_details bd ON db.business_id = rvs.business_id
WHERE rvs.review_id = '$approveReviewID'