I want to write a MySQL Query that updates Table A based on logic test made on Table B.
I want to show [visible: yes] only products with discount > 40%
Discount logic test: [100/(old_price/price)] > 40 . The query is for use on PhpMyAdmin (WordPress)
Table A (product status)
product_id visible
1 yes
2 no
3 yes
4 no
Table B (product details)
product_id meta_key meta_value
2 price 550
2 old_price 600
1 price 200
1 old_price 400
4 price 300
4 old_price 350
3 price 100
3 old_price 300
update product_status
set visible = 'yes'
where product_id in ( select product_id, (100/(max(old_price)/max(price))) as discount
from ( select product_id, meta_value as old_price, null as price
from product_details
where meta_key = 'old_price'
union
select product_id, null, meta_value
from product_details
where meta_key = 'price' ) as checkit
where (100/(max(old_price)/max(price)) > 40
group by product_id));
This might give what you want:
select product_status.product_id,
case when (1.00*c.meta_value)/(1.00*b.meta_value) < 0.6 then 1 else 0 end as visible
from product_status
inner join
(select *
from product_details
where meta_key = 'old_price') b
on product_status.product_id = b.product_id
inner join
(select *
from product_details
where meta_key = 'new_price') c
on product_status.product_id = c.product_id
If it does, substitute the appropriate UPDATE statement for the SELECT statement.
This is the answer Mr. Dhruv Saxena kindly sent me and works perfectly:
UPDATE product_status ps
INNER JOIN product_details pd1
ON ps.product_id = pd1.product_id
AND pd1.meta_key = 'price'
INNER JOIN product_details pd2
ON ps.product_id = pd2.product_id
AND pd2.meta_key = 'old_price'
SET ps.visible =
CASE
WHEN (100/(pd2.meta_value/pd1.meta_value)) > 40.00
THEN 'yes'
ELSE
'no'
END;
He even sent me a demo test: http://rextester.com/OWRQZE95139
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.