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

SQL único para recuperar información diferente de diferentes tablas

Si es posible que algunos datos no existan en una de las tablas, en lugar de usar INNER JOIN deberías usar LEFT JOIN :

SELECT content.loc,content.id,content.title,
   -- USE function COALSESCE will show 0 if there are no 
   -- related records in table voting_count
    COALESCE(voting_count.up, 0) as votes_up,  
    COALSESCE(voting_count.down, 0) as voted_down
FROM content LEFT JOIN voting_count 
    ON content.id = voting_count.unique_content_id 
ORDER BY content.id DESC