This statement
SELECT * FROM TempTable t1
gives me this output:
id idIndice valor
----------------------------
13 11 11111111
13 12 11111
14 11 11111111
14 12 11111
16 12 11111
Now with a little change:
SELECT * FROM TempTable t1
WHERE (select count(*) from TempTable t2 where t1.id = t2.id AND t1.valor != t2.valor) = 1
I get this
id idIndice valor
----------------------------
13 11 11111111
13 12 11111
14 11 11111111
14 12 11111
t1 and t2 are references to the same tempTable and I can understand t1.id = t2.id but this:
t1.valor != t2.valor
is just too much for me. How come same columns as t1 and t2 are references for the same tables can be different?
And why this “= 1” at the end
What the second query essentially does is “Select any rows for which the number of rows with the same ID and a different valor is 1” i.e. “Select any rows that have duplicate id’s“
That is why both rows with
id=13are selected, as they are each others duplicate, and also why both rows forid=14are selected.Note that if there are three rows with a same ID, none of them will be selected, because their number of duplicate id’s wont be 1, but 2.