Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

update column to two based on condition

I am trying to modify ONE column, I want to set some rows as true the others convert them to false

update products set on_sale=False where status=1 and seller=test;
update products set on_sale=true Where price > 100 and status=1 and seller=test;

the above works, but I believe it can be done in 1 query, I.e something like this

\\ python syntax for the if condition
update prodcuts set on_sale=(True if price > 100 else False) WHERE status=1 and seller=test 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could do a single update with the help of a CASE expression:

UPDATE products
SET on_sale = CASE WHEN price > 100 THEN True ELSE False END
WHERE status = 1 AND seller = test;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading