从重复的id中获取一个id(所有id都具有不同的值)&仅使用一个值永久更新唯一ID(应用条件)

时间:2017-06-15 17:41:58

标签: mysql sql

我似乎无法解决这个问题。我需要永久更新表格。我需要在这里应用2个条件 -

(1)当同一个id同时具有“Bachelor's”和“Masters”两个值时,我需要只有一次id,它应该包含“Bachelor's”的学位。

(2)当同一个id的值为“Bachelor's”,“Masters”和“PHD”时,我需要只有一次id,它应该包含“Masters”的学位。

id         degree 
1           bachelor
2           master
3           bachelor
1           master
2           bachelor 
2           phd 

我想要这样的结果 -

1        bachelor
2        master 
3        bachelor 

希望它在表格中永久更新,以便我可以将其加入其他表格。

2 个答案:

答案 0 :(得分:0)

好的,首先我尝试做你所描述的,我做了这个

drop table test7;

create table test7 (
id_num number,
education varchar(25)
);

insert into test7 values(1,'bachelor');
insert into test7 values(2,'masters');
insert into test7 values(3,'bachelor');
insert into test7 values(1,'masters');
insert into test7 values(2,'bachelor');
insert into test7 values(2,'phd');

-- this finds those id numbers that have duplicates
--select count(id_num),id_num from test7 group by id_num having count(id_num) > 1;

-- this shows the duplicate ids, their id number, and their education
--select c.multi,c.id_num,t.education from (select count(id_num) multi,id_num from test7 group by id_num having count(id_num) > 1) c left join test7 t on c.id_num=t.id_num;

-- this finds the distinct id number that has 2 duplicates
--select distinct id_num from (select c.multi,c.id_num,t.education from (select count(id_num) multi,id_num from test7 group by id_num having count(id_num) > 1) c left join test7 t on c.id_num=t.id_num) where multi = 2;

-- when there are two records with the same id, get rid of the masters one
delete from test7 where id_num = (select distinct id_num from (select c.multi,c.id_num,t.education from (select count(id_num) multi,id_num from test7 group by id_num having count(id_num) > 1) c left join test7 t on c.id_num=t.id_num) where multi = 2) and education = 'masters'; 

-- when there are 3 records with the same id, get rid of bachelor and phd
delete from test7 where id_num = (select distinct id_num from (select c.multi,c.id_num,t.education from (select count(id_num) multi,id_num from test7 group by id_num having count(id_num) > 1) c left join test7 t on c.id_num=t.id_num) where multi = 3) and education = 'bachelor' or education = 'phd'; 

    select * from test7;

    ID_NUM EDUCATION               
---------- -------------------------
         1 bachelor                 
         2 masters  
         3 bachelor     

这适用于您所描述的内容,但它并不适用于变体。

但是,通常当你想要在id号上连接表时,你需要不同的id / value对,所以我添加了一个适用于它的解决方案...

drop table temp;
drop table test6;
drop sequence temp_id_seq;


create table test6 (
id_num number,
education varchar(25)
);

insert into test6 values(1,'bachelor');
insert into test6 values(2,'master');
insert into test6 values(3,'bachelor');
insert into test6 values(1,'master');
insert into test6 values(2,'bachelor');
insert into test6 values(2,'phd');

create table temp (
temp_id number,
education varchar(25)
);

create sequence temp_id_seq
minvalue 1
maxvalue 100000
start with 1
increment by 1
nocycle;

create or replace trigger created_temp_id
before insert on temp
for each row
begin
:new.temp_id := temp_id_seq.nextval;
end;
/

insert into temp(education) select distinct education from test6;

select * from temp;

delete from test6;

insert into test6 select * from temp;

select * from test6;

-- which results in this:

    ID_NUM EDUCATION               
---------- -------------------------
         1 master                   
         2 phd                      
         3 bachelor     

希望这有用:)

答案 1 :(得分:0)

你可以试试这个:

SELECT a.id,
 CASE 
  WHEN FIND_IN_SET('bachelor, master', a.groups) > 0 THEN 'bachelor'
  WHEN FIND_IN_SET('bachelor, master, phd', a.groups) > 0 THEN 'master'
 END AS grouping
FROM(
 SELECT
  id,
  GROUP_CONCAT(degree ORDER BY degree ASC SEPARATOR ', ') groups
 FROM `tablename`
 GROUP BY id) a