MySQL Trigger Code to enter statement based on another value -
edit **********
i couldn't below code work have tried run in diagram tool on workbench. code
create definer = current_user trigger `measureup_data`.`bmd_results_before_insert` before insert on `bmd_l_arm` each row begin insert bmd_results values ( new.bmd_l_arm_typ case new.l_arm_tscore when < -2.5 'osteoporosis' when > -2.4 < -1.0 'osteopenia' when > -1.0 'normal' else null end); end
im getting red crosses though code. values automatically entered data.bmd_l_arm
, want field in data.bmd_results
labelled bmd_l_arm_typ
have text entry based on numerical result pushed l_arm_tscore
thanks ongoing help!
i new coding game , have been creating db on mysql add value company. have created db schema , want have field in 1 of table insert value based on entry comes table. medical reporting database , diagnosis appear in text on in field entry.
this table values appear, more specially column (for example) bmd_sp_typ
inset varchar (i specify result) based on numerical result in different table.
create table if not exists `measureup_data`.`bmd_results` ( `access_no` int not null, `bmd_sp_typ` varchar(45) null, primary key (`access_no`), index `pat_id_fk_idx` (`pat_id` asc), index `fk_site_bmd_site_idx` (`site_id` asc), unique index `pat_id_unique` (`pat_id` asc), unique index `site_id_unique` (`site_id` asc), unique index `access_no_unique` (`access_no` asc), constraint `fk_demo_results_id` foreign key (`pat_id`) references `measureup_data`.`patient_demo` (`pat_id`) on delete no action on update no action, constraint `fk_site_bmd_site` foreign key (`site_id`) references `measureup_data`.`site_cont` (`site_id`) on delete no action on update no action) engine = innodb
the table numerical result found below. value spine_total_bmd
x
y
inserted bmd_sp_type
, alternatively, if spine_total_bmd
a
b
inserted.
create table if not exists `measureup_data`.`bmd_spine` ( `access_no` int not null, `spine_total_bmd` int null, `spine_total_tscore` int null, `spine_total_zscore` int null, `spine_total_peakref` int null, `spine_total_agemat` int null, primary key (`access_no`), constraint `fk_bmd_spine_access` foreign key (`access_no`) references `measureup_data`.`bmd_results` (`access_no`) on delete no action on update no action) engine = innodb
any coding noob appreciated.
you can write insert trigger -
create trigger trigger before insert on bmd_spine each row begin insert bmd_results values ( new.access_no, case new.spine_total_bmd when 1 'y' when 2 'b' else null end); end
also, can select data table , change values on fly, , not store duplicated values. here example -
select access_no, case spine_total_bmd when 1 'y' when 2 'b' else null end bmd_spine;
Comments
Post a Comment