Los resultados de la consulta no coinciden con los resultados deseados

Given a Relation Person(id,name,momid,dadid)

Return all siblings (ie those with common momid,dadid)

por ejemplo 1,2

So in this query i need to result all siblings in a row, with one pair of siblings per row.

I have the solution not very sure if it works..it is

select p1.id,p2.id 
from person p1,person p2 
where p1.momid=p2.momid 
and p1.dadid=p2.dadid

I believe it is the write solution but it resulted a row 1,1 so is the query logic wrong ?

preguntado el 10 de marzo de 12 a las 07:03

2 Respuestas

Both p1 and p2 can be the same person in your query since you're not saying they should be different. You can just add that condition and it should work just fine;

SELECT p1.id, p2.id 
FROM person p1, person p2 
WHERE p1.momid = p2.momid 
  AND p1.dadid = p2.dadid
  AND p1.id <> p2.id

respondido 10 mar '12, 07:03

Otherwise correct, but you have to filter out records which are referring to the same person by adding AND p1.id <> p2.id al igual que:

select p1.id,p2.id 
from person p1,person p2 
where p1.momid=p2.momid 
and p1.dadid=p2.dadid and p1.id <> p2.id

respondido 10 mar '12, 07:03

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