2017-05-16 4 views
-1

어떻게이 쿼리를 수행 할 수 있습니까? 학생 데이터와 학부모 정보를 한 줄로 보여줘야합니다. 이것은 쿼리입니다.이 쿼리는 어떻게 수행합니까?

select da.nombre, da.apPaterno, da.apMaterno, f.nombre, f.aPaterno, 
f.aMaterno, f.parentesco , fa.nombre, fa.aPaterno, fa.parentesco from 
datoaspirante as da 
left join familiar as f on da.familiarid = f.datoaspirante 
left join familiar as fa on da.familiarid = fa.datoaspirante 
where fa.parentesco = 'Madre' and f.parentesco = 'Padre'; 

하지만,이 쿼리는 아버지와 어머니가 있고 난 단지 아버지가 나 단지 어떤 생각 부모가없는 어머니 또는 학생이 학생을 보여줄 필요가 학생들에게 보여?

This is the student table:

이 부모 테이블은 다음과 같습니다 :

은 학생 테이블

This is the parent table:

+0

두 테이블에 대한 샘플 데이터와 그에 따른 예상 출력을 제공하십시오. – Utsav

답변

0

당신은 샘플 데이터를 언급하지 않은 것처럼, 난 단지이 가정하고 무엇을 당신 필요.

select d.*,f.* 
,case 
    when f.datoaspirante is null 
    then 'No parents' 
    else 'Single parent' 
end as type 
from 
datoaspirante d 
left join 
(select datoaspirante from familiar 
group by datoaspirante 
having count(*)<2 
) f 
on d.familiarid = f.datoaspirante 
1

당신은 아버지와 어머니 중 하나가 조인에 where fa.parentesco = 'Madre' and f.parentesco = 'Padre';

이동 조건을 설정 밤은 모든 빈 행을 필터링하고, 원하는 동작을 얻어야한다.

SELECT da.nombre, da.apPaterno, da.apMaterno, f.nombre, f.aPaterno, 
f.aMaterno, f.parentesco, fa.nombre, fa.aPaterno, fa.parentesco 
FROM 
datoaspirante AS da 
LEFT JOIN familiar AS f ON da.familiarid = f.datoaspirante and f.parentesco = 'Padre' 
LEFT JOIN familiar AS fa ON da.familiarid = fa.datoaspirante and fa.parentesco = 'Madre'; 
+0

많은 사람에게 감사합니다 !! – David

관련 문제