简体   繁体   English

[MySQL]根据条件插入值

[英][MySQL]Insert a value depending on a condition

I'm studying MySQL so I'm a bit new to all this stuff, but the last time I asked you guys, you proved to be really helpful, so perhaps you can help me again because I haven't been able to find the answer on my own. 我正在学习MySQL,所以我对所有这些东西都有些陌生,但是上次我问你们时,您真的很有帮助,所以也许您可以再次帮助我,因为我一直找不到我自己回答。

Ok, so the thing is, I need to add a new column to an already existing table, and it should be like: 好的,问题是,我需要向已经存在的表中添加一个新列,它应该像这样:

ALTER TABLE `medicos` 
ADD COLUMN `tipo_medico` VARCHAR(20) NOT NULL DEFAULT 'indiferente' AFTER `sueldo`;

Now I'm supposed to run an INSERT instruction to add the data there. 现在,我应该运行一条INSERT指令在其中添加数据。 After adding that column, the table looks like this: 添加该列后,该表如下所示:

在此处输入图片说明

These values equal to: Medic_number, Surnames, Speciality, Day of birth, University, Wage and Medic_type (the new column I've added). 这些值等于:Medic_number,姓氏,专业,出生日期,大学,工资和Medic_type(我添加的新列)。 The data I add on all the columns except the last one doesn't really matter, but the last column must be filled following the next conditions: 我在除最后一列之外的所有列上添加的数据并不重要,但是必须在以下条件下填充最后一列:

-By default, it'll be "indiferente" (that's working). -默认情况下,它将是“ indiferente”(正在运行)。

-If a medic has studied in an university ending in -a (ie Valencia), it'll say "Excellent". -如果医务人员在以-a结尾的大学(即瓦伦西亚)学习,它将说“非常好”。

-If a medic is 40 years old or older, it'll say "Expert". -如果医生年满40岁或以上,则会显示“专家”字样。

Now, I know how to do these conditions ("... university like = '%a'", etc), but I don't know how to add that into the INSERT instruction. 现在,我知道如何执行这些条件了(例如,“ ... University like ='%a'”等),但是我不知道如何将其添加到INSERT指令中。 This is what I've gotten so far: 到目前为止,这是我得到的:

insert into medicos values ('A021', 'Apellidos :D', 'Loquesealogia', '1970-03-14', 'Valencia', '3500');

That adds everything but the last column, which is the one where I'm lost. 除了最后一栏,这就是我迷失的那一栏。 What can I do to tell the INSERT instruction to, depending on which of the previously named conditions are true, add one value or another? 我可以做什么来告诉INSERT指令,根据前面提到的哪个条件为真,添加一个值或另一个值?

I hope I've been clear enough, I'm not a native English speaker so I'm sorry if that wasn't good enough. 我希望我已经足够清楚了,我不会说英语,所以如果这还不够好,我感到抱歉。 Also I've tried to format the codes this time, I hope that'll work. 另外,我这次尝试格式化代码,希望可以。

Thanks. 谢谢。

This seems to be a case when we need to apply a logic on column values before a record is inserted. 当我们需要在插入记录之前对列值应用逻辑时,似乎是这种情况。 I would suggest creating a BEFORE INSERT trigger on this table, which will be automatically executed by MySql before each record is inserted. 我建议在此表上创建一个BEFORE INSERT trigger ,该BEFORE INSERT trigger将在插入每条记录之前由MySql自动执行。

We can write the logic to determine the value of last column depending upon values supplied for the other columns. 我们可以编写逻辑以根据为其他列提供的值来确定最后一列的值。 Have a look at this link for trigger example. 请看链接以获取触发器示例。

If the requirement here is to do a one time bulk insert then, we can drop this trigger once insertion is complete. 如果这里的要求是一次性批量插入,那么一旦插入完成,我们就可以删除该触发器。

I would advise you to do it either with BEFORE INSERT trigger as Darshan Mehta recommends, or do your logic in the programming side, or with a stored procedure. 我建议您按照Darshan Mehta的建议,使用INSERT触发器之前执行此操作,或者在编程端执行逻辑,或使用存储过程。

Still it is doable at query time, so to answer your question specifically, try something like this: 仍然可以在查询时执行,因此要具体回答您的问题,请尝试如下操作:

INSERT INTO medicos (num_colegiado, apellidos, especialidad, fecha_nac, universidad, sueldo, tipo_medico) SELECT 'A021', 'Apellidos :D', 'Loquesealogia', '1970-03-14', 'Valencia', '3500', IF('Loquesealogia' like '%a','Excellent',IF(DATE_ADD('1970-03-14', interval 40 YEAR)>NOW(),'Expert','indiferente'))  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM