Encuentra 1 columna la misma otra columna diferentes filas de la misma tabla

Is the query below correct to find columns for which the HashCode is the same but PageUrl different rows? This would mean basically collisions in my database planning.

select T1.PageUrl,T1.HashCode from tblPages as T1 inner join
(select PageUrl,HashCode from tblPages group by PageUrl,HashCode) as T2 on
T1.PageUrl!=T2.PageUrl and T1.HashCode=T2.HashCode

I am using 64 bit HashCode.

MS-SQL 2008 R2

preguntado el 01 de febrero de 12 a las 22:02

FYI != is MySQL, <> is MS SQL -

I am always using != and it always works with MSSQL. Would there be any difference ? -

@Splash-X: For your information, <> is the standard SQL "Not Equal To" operator. It should work in all SQL products (and off course in both MySQL and SQL-Server). The != is not standard but works in MySQL and Servidor SQL -

2 Respuestas

Usaría un CROSS JOIN con un WHERE clause rather than your INNER JOIN.

SELECT T1.PageUrl,T1.HashCode FROM tblPages AS T1
WHERE T1.PageUrl <> T2.PageUrl AND T1.HashCode = T2.HashCode

The reason for this is that the optimizer will give you a better query plan with the cross join. Enlace a un ejemplo.

Respondido 02 Feb 12, 02:02

Pretty close, i would go with this:

select T1.PageUrl,T1.HashCode from tblPages as T1 
inner join (select distinct PageUrl,HashCode from tblPages) as T2 
on T1.PageUrl <> T2.PageUrl and T1.HashCode=T2.HashCode

Respondido 02 Feb 12, 02:02

How is that different than the OP's query? - ypercubeᵀᴹ

His question is whether his query is right, and it is. I suggested the distinct rather than the group by in the inner query and to use the SQL-Server <> rather than the != - Sparky

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.