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

Mysql 仅从重复id中获取一个id(所有id都具有不同的值)&;仅使用一个值永久更新唯一id(应用条件),mysql,sql,Mysql,Sql,我似乎无法解决这个问题。我需要在表中永久更新此内容。我需要在这里应用2个条件- (1) 当同一个id同时具有“学士”和“硕士”两个值时,我只需要拥有一次id,它应该包含“学士”学位 (2) 当同一个id具有“学士”、“硕士”和“博士”的值时,我只需要拥有一次id,它应该包含“硕士”的学位 id degree 1 bachelor 2 master 3 bachelor 1 master 2

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

(1) 当同一个id同时具有“学士”和“硕士”两个值时,我只需要拥有一次id,它应该包含“学士”学位

(2) 当同一个id具有“学士”、“硕士”和“博士”的值时,我只需要拥有一次id,它应该包含“硕士”的学位

id         degree 
1           bachelor
2           master
3           bachelor
1           master
2           bachelor 
2           phd 
我想要这样的结果-

1        bachelor
2        master 
3        bachelor 

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

好的,首先我试着做你描述的,我做到了

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/值对,所以我添加了一个解决方案,该解决方案也适用于此

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     
希望这对您有所帮助:)

您可以尝试以下方法:

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

你试了什么?你面临的问题是什么?您在从表中删除条目或更新任何特定条目时是否遇到问题?