By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE table1 (
`ID` VARCHAR(100),
`Val` VARCHAR(100),
`Val2` VARCHAR(100)
);
INSERT INTO table1
(`ID`, `Val`, `Val2`)
VALUES
('1','4788','90'),
('2','4788','90'),
('10','4788','10'),
('20','111','10'),
('30','111','10'),
('57','89','89111'),
('59','89','89111'),
('60','89','10'),
('2','900','7000'),
('4','900','7001');
select t1.* from table1 t1
where exists (select 1 from table1 where id <> t1.id and val = t1.val)
and exists (
select 1 from table1
where val = t1.val and val2 in (select val2 from table1 group by val2 having count(*) > 1)
)
ID | Val | Val2 |
---|---|---|
1 | 4788 | 90 |
2 | 4788 | 90 |
10 | 4788 | 10 |
20 | 111 | 10 |
30 | 111 | 10 |
57 | 89 | 89111 |
59 | 89 | 89111 |
60 | 89 | 10 |