sql - How to check if a record exists then return 1 else return 0 in MySQL -


i have phone numbers database. in database have column called "main_number" , tinyint(1) type user 0 not main number or 1 main number.

the issue having bulk insert 1 database using stored procedure.

the business rules states 1 phone number can marked main phone number. now, need check if there record marked "main_number" if "main_number" should have value 0. if there no record found should make main_number = 1.

the following defention of table

create table `contact_numbers` (  `number_id` int(10) unsigned not null auto_increment,  `account_id` int(10) unsigned default null,  `person_id` int(11) default null,  `contact_number` char(15) not null,  `contact_extension` char(10) default null,  `contact_type` enum('office','fax','reception','direct','cell','toll free','home') not null default 'office',  `contact_link` enum('account','pdm','other') not null default 'account',  `status` tinyint(1) not null default '1' comment '0 = inactive, 1=active',  `main_number` tinyint(1) not null default '0' comment '1 = main phone number',  `created_on` datetime not null default current_timestamp,  `created_by` int(11) not null,  `modified_on` datetime default null,  `modified_by` int(11) not null default '0',  `external_id1` char(18) default null comment 'client''s account id',  `external_id2` char(18) default null comment 'client''s person id',  primary key (`number_id`),  unique key `external_id1` (`external_id1`),  unique key `external_id2` (`external_id2`),  unique key `person_id_2` (`person_id`,`contact_type`),  unique key `person_id_3` (`person_id`,`contact_number`,`contact_extension`),  unique key `account_id_4` (`account_id`,`contact_number`,`contact_extension`),  key `account_id` (`account_id`),  key `person_id` (`person_id`),  key `account_id_2` (`account_id`,`contact_link`,`main_number`),  constraint `cn_account_id` foreign key (`account_id`) references `accounts` (`account_id`) on delete cascade on update cascade,  constraint `cn_person_id` foreign key (`person_id`) references `contact_personal` (`person_id`) on delete cascade on update cascade ) engine=innodb auto_increment=114851 default charset=utf8 

this query need add check see if record exists account_id has main_number = 1.

select ac.account_id, a.phone, 'office', 'account', 1 created_by rdi_ge_dev.account inner join finaltesting.accounts ac on ac.external_id1 = a.sfdc_account_id 

note: don't want add new unique index , don't want user on duplicate key update.

by performing left join against contact_numbers table , looking null after limiting results main_number = 1 can use case in select list assign 1 or 0 accordingly.

select   ac.account_id,   a.phone,   'office',   'account',   1 created_by,   /* null in left join means doesn't exist */   case when cn.number_id null 1 else 0 end main_number   rdi_ge_dev.account   inner join finaltesting.accounts ac on ac.external_id1 = a.sfdc_account_id   left join contact_numbers cn on a.account_id = cn.account_id    /* limit left join main_number = 1 */   cn.main_number = 1 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -