L
L
lightalex2016-11-20 00:22:57
MySQL
lightalex, 2016-11-20 00:22:57

How to substitute a value depending on the presence of a record in another MySQL table?

Good day
There is a table Pricelist:
8ee17440ed504a509009ba0272a241b1.png
and a table Lots:
cffd6fb1ea104b53960f1b6fab042f12.png
Task: if for a row from the table Pricelist , where id = 2 (id is substituted by PHP), there is a row in the table Lots , where the fields Pricelist.ID = Lots.ID_of_price AND Pricelist.prices = Lots.prices , then update Own_Price = 1 (for the row where id = 2), otherwise update Own_Price = 0 (for the row where id = 2)
Is it possible to do this somehow using only one MySQL query (request is sent from PHP) ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
entermix, 2016-11-20
@lightalex

You can, use the IF statement , or CASE
Something like this:

UPDATE Pricelist SET Own_Price = if (EXISTS(SELECT `id` FROM `Lots` WHERE `Pricelist`.`ID` = `Lots.`ID_of_price` AND `Pricelist`.`prices` = `Lots`.`prices` LIMIT 1),  1, 0) WHERE `id` = 2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question