Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

sql self-join table remove linhas duplicadas


Com seus identificadores:
SELECT a.userId , a.LanguageId, b.LanguageId
  FROM knownlanguages a inner join knownlanguages b  
    on a.userID=b.userID and a.LanguageId < b.LanguageId

Teste:Tabela Fot:
create table t ( u int, l int);

insert into t values 
(    1,               2),
(    1,               7),
(    1,               8),
(    2,               10),
(    2,               3);

A consulta é:
select t1.u, t1.l as l1, t2.l as l2
from t t1 inner join t t2
   on t1.u = t2.u and t1.l < t2.l

( Resultados )