Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

exibindo todos os registros de uma tabela e registros correspondentes de outra


Use uma junção externa:
select 
    a.activityName, 
    coalesce(sum(pt.remainderPoints), 0) points
from 
    activities a left outer join pointsTable pt on 
        pt.activityName = a.activityName and pt.UserID = 2
group by a.activityName

ou uma subconsulta:
select
    activities.ActivityName,
    coalesce(
        (select sum(RemainderPoints) from pointsTable where pointsTable.UserId = 2 and pointsTable.ActivityName = activities.ActivityName)
    , 0) points
from
    activities