Mysql 如何进行此查询

Mysql 如何进行此查询,mysql,Mysql,如何进行此查询。我需要在一行中显示学生数据和家长信息。这就是问题所在 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.datoaspiran

如何进行此查询。我需要在一行中显示学生数据和家长信息。这就是问题所在

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';
但是这个查询只显示有父亲和母亲的学生,我需要显示只有父亲或母亲的学生,或者没有父母的学生

这是学生表:

这是父表:


由于您没有提到示例数据,我只是假设这是您想要的

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
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';

您将筛选出所有未设置父和母的空行,其中fa.parentesco='Madre'和f.parentesco='Padre'

将条件移动到联接时,应获得所需的行为

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
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';

给出两个表的示例数据以及基于此的预期输出。