sql >> Base de Datos >  >> RDS >> Mysql

mostrar todos los registros de una tabla y los registros coincidentes de otra

Utilice una combinación 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

o una subconsulta:

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