Mysql judges the field assignment according to the if condition

Because the author spent a lot of time searching for relevant information on the website yesterday, but most of the information did not solve the problem. Either the format is wrong, or the if written in the title actually uses the case judgment syntax, or the query result is only assigned to the field during the query. In fact, the fields in the table have not been changed, and there are certain problems, so in order to help friends in the future. Decided to give me a correct way here. This judgment can change the field value exactly according to the if condition judgment, so update.

The key code is as follows:
update table name set field name=if(condition, value to be assigned, field name consistent with the previous);

Take a complete creation process as an example: In this example, D is a quantity representing time. T is a table name, and V is an integer variable

delimiter $$
create procedure update()
update T set V=if(date(D)>'2022-11-10',1,V);
end$$
delimiter;
call update();

Hope to be helpful to readers

Guess you like

Origin blog.csdn.net/m0_51980089/article/details/127796697